spring入门

Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架

 

menu-spring学习步骤:
1.资源jar包引入
2.网上教程
3.踏过的坑
4.写文档理解
5.文件输出

01-参考资源
http://blog.csdn.net/xieyuooo/article/details/8473503

spring  xml约束
http://blog.csdn.net/qq_31776219/article/details/52275722/

 

02-踏过的坑

error:找不到元素 'bean' 的声明
原因:我不清楚xml配置结构
头部引入spring的xsd的版本或路径的问题,更换即可

 

03-正文

spring入门书:spring In Action
IOC 、DI、AOP等Spring核心概念。

 

概念理解:

Spring容器:Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器

Spring框架:典型地是在一个XML文件里。Spring也提供了很多基础功能(事物管理、持久化框架集成等等),将应用逻辑的开发留给了你。

SpringMVC——Spring的作用是整合,但不仅仅限于整合

客户端发送请求,

服务器控制器(由DispatcherServlet实现的)完成请求的转发,

控制器调用一个用于映射的类HandlerMapping,

一个完整的mvc流程,完美

Spring AOP(面向切面)Spring提供了面向切面编程的丰富支持

 

 

ioc(控制反转):
由spring来负责控制对象的生命周期和对象间的关系
传统的程序开发:
在一个对象中,如果要使用另外的对象,就必须得到它(自己new一个,或者从JNDI中查询一个),
使用完之后还要将对象销毁(比如Connection等),对象始终会和其他的接口或类藕合起来。
我可以ioc提出一个列表,对象属性
如果ioc给我们的人选不符合要求,我们就会抛出异常。
整个过程不再由我自己控制,而是有ioc这样一个类似容器的机构来控制。


di(依赖注入)
在系统运行中,动态的向某个对象提供它所需要的其他对象。
这一点是通过DI(Dependency Injection,依赖注入)来实现的。

 

 

描述:

他有一个xx类(phz.springframework.test.Cat)
实现了xx接口(phz.springframework.test.Animal)
获取spring的ApplicationContext几种方式

 

code:

xx接口(phz.springframework.test.Animal)

package phz.springframework.test;

public interface Animal {
     public void say();
}

关键词:Spring容器加载

 package phz.springframework.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class Cat implements Animal {
    private String name;
    @Override
    public void say() {
        System.out.println(" i am "+this.name+"!");
        
        
    }
    public void setName(String name) {
        this.name = name;
    }
    
    public static void main(String[] args) {
        
//    加载完Spring容器 -xml wenjian    
        
        ApplicationContext context = new FileSystemXmlApplicationContext(   
                "WebContent/WEB-INF/etc/spring/applicationContext.xml");
        
       Animal animal = (Animal) context.getBean("animal");   
       animal.say();
        
      Animal animal2 = (Animal) SpringContextHolder.getBean("animal");
      animal2.say();
    }
}



关键词:ApplicationContextAware,annotation注解


package phz.springframework.test;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/***
 * 1.实现ApplicationContextAware接口
 * 2.在Spring的配置文件中配置这个类,
 * Spring容器会在加载完Spring容器后把上下文对象
 * 调用这个对象中的setApplicationContext方法
 * 3.在web项目中的web.xml中配置加载Spring容器的Listener
 *
 * ***/

public class SpringContextHolder implements  ApplicationContextAware {

    private static ApplicationContext applicationContext;
    

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        
        SpringContextHolder.applicationContext = applicationContext;  
    }
     public static ApplicationContext getApplicationContext() {
          return applicationContext;  
     }
     public static Object getBean(String beanName){
         return applicationContext.getBean(beanName);
     }
     public static <T>T getBean(String beanName , Class<T>clazz) {  
            return applicationContext.getBean(beanName , clazz);  
        }
    

}



 
 
关键词配置文件

applicationContext.xml中:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
        <!-- 实现ApplicationContextAware -->
        <bean id="SpringContextHolder" class="phz.springframework.test.SpringContextHolder"></bean>
        <!-- 业务bean列表-->
        <bean id="animal" class="phz.springframework.test.Cat">  
            <property name="name" value="kitty" />  
        </bean>
 
    
</beans>



web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>KmwalletApp</display-name>


    <!-- spring-DispatcherServlet-->
    
    <servlet>  
            <servlet-name>spring</servlet-name>  
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
            <load-on-startup>1</load-on-startup>  
    </servlet>  
    
    <!--  
    spring-ContextLoaderListener
    RequestContextUtils.getWebApplicationContext在spring 3当中,
        如果没有启动ContextLoaderListener(当然你可以配置监听),是不会成功的。
    -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
</web-app>

 

posted @ 2017-07-19 14:49  alan-alan  阅读(126)  评论(0编辑  收藏  举报