1:Spring 是什么 spring 是一个开源的开控制反转(IOC)和面向切面(AOP)的容器框架。 它的主要目的是简化企业开发。 2:IOC: public class PersonServiceBean{ private PersonDao personDao=new PersonDao(); public void save(Person person){ personDao.save(person); } } 如上:PersonDao 是在"应用内部"创建及维护的, 所谓的IOC就是应用本身不负责依赖对象的创建及维护, 对象的创建及维护有外部容器负责,这样控制权就由"应用"转移到 "外部容器",控制权的转移就是所谓的反转; 3:依赖注入: 当我们把依赖对象交给外部容器负责创建,那上面的程序该改成如下: public class PersonServiceBean{ private PersonDao personDao; //通过构造器参数,让容器创建好的依赖对象注入进PersonServiceBean //当然也可以通过setter方法进行注入 public PersonServiceBean(PersonDao personDao){ this.personDao=personDao; } public void save(Person person){ personDao.save(person); } } 所谓依赖注入就是指:在运行期,由外部容器动态的将依赖对象注入到组件中。 4:spring的好处: (1)降低了action-->service-->dao层之间的耦合度, 以前action层需要调用service层(及在action中的new service层), service层需要调用dao层; 如上面"2:IOC"中的程序一样(private PersonDao personDao=new PersonDao();) 如果不提供PersonDao的话改程序编译都不会成功的,这样就以看成各层之间是紧密 耦合的; (2)提供了很多的服务:如事务控制等 (3)容器提供了单例模式 (4)提供了aop:权限拦截等 (5)提供了众多的辅助类:如jdbcTemplate等 (6)对主流框架提供了集成支持 5:spring 管理bean(也就是类) 只需在spring的配置文件中配置一个<bean>,spring就会自动的去实例化该类 如:<bean id="personService" class="com.lid.service.PersonServiceBean"/> 6:bean的作用域: 默认是单利模式,及只创建一次bean 如果想没调用一次就创建一个新的bean对象的话,只需指定一下作用域即可如下: <bean id="personService" class="com.lid.service.PersonServiceBean" scope="prototype"/> 7:bean的创建时: (1)如果作用域的范围是默认的:单例模式时 bean是在spring加载时创建的; 如果改成延时实例化<bean id="personService" class="com.lid.service.PersonServiceBean" lazy-init="true"/> 则bean是在spring容器实例化以后,我们调用spring的getBean方法是实例化的 (2)如果作用域prototype时,bean是在spring容器实例化以后,我们调用spring的getBean方法时实例化的; 如果想让spring管理的所有bean都延时初始化的话如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="true"> </beans> 就是在beans标签中添加default-lazy-init="true"; 8:为spring指定创建bean实例是执行bean实例的某个方法,销毁时执行某方法: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="true" default-init-method=""> <!-- 这时候该bean由spring来创建 --> <bean id="personService" class="com.lid.service.PersonServiceBean" init-method="" destroy-method=""/> </beans> 9:spring的依赖注入: (1)通过setter方法 类: public class PersonServiceBean { private PersonDao personDao; public void save(){ System.out.println("-----spring test----"); } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } } spring配置文件: <bean id="personDao" class="com.lid.dao.PersonDao"/> <bean id="personService" class="com.lid.service.PersonServiceBean"> <property name="personDao" ref="personDao"></property> </bean> 解析:通过ref后面的名称在spring中找到该对象,然后注入给PersonServiceBean对象中的 属性名为personDao的这个属性; 当然也可如下: <bean id="personService" class="com.lid.service.PersonServiceBean"> <property name="personDao"> <bean class="com.lid.dao.PersonDao"></bean> </property> </bean> 这两种的优缺点: 第一种 使用ref这样<bean id="personDao" class="com.lid.dao.PersonDao"/> 只在外面定义的,可以被公用; 第二种 在在内部定义个,只供自己用; (2)通过构造方法 public class PersonServiceBean { private PersonDao personDao; private String name; public PersonServiceBean(PersonDao personDao,String name){ this.personDao=personDao; this.name=name; } public void save(){ System.out.println("-----spring test----"); } } spring配置文件: <bean id="personDao" class="com.lid.dao.PersonDao"/> <bean id="personService" class="com.lid.service.PersonServiceBean"> <constructor-arg index="0" type="com.lid.dao.PersonDao" ref="personDao"/> <constructor-arg index="1" value="zhangsan"/> </bean> 解析: index:只的是构造函数中的第一个参数 type:指的是参数的类型,基本类型不用指定 ref,value:指的是将什么类型(值)注入; (3)通过注解注入: 使用@Autowire或者@Resource注解进行装配 @Autowire 按类型进行装配------去配置文件中找与标注的类型相同的bean,然后注入; @Resource 按照名称进行装备,当找不到与名称匹配的bean才会按类型---也是去配置文件中找 配置文件要改成如下: <?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> </beans> 类: public class PersonServiceBean { @Resource private PersonDao personDao; private String name="xxx"; public PersonServiceBean(PersonDao personDao,String name){ this.personDao=personDao; this.name=name; } public void save(){ System.out.println("-----spring test----"); } } 配置文件: <?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="personDao" class="com.lid.dao.PersonDao"/> <bean id="personService" class="com.lid.service.PersonServiceBean"/> </beans> 10:spring自动扫描和管理bean spring在类路径下寻找标注了@Component、@Service、@Controller 、@Repository注解的类,并把这些类纳入进spring容器中进行管理; @Service用于标注业务层组件 @Controller用于标注控制层组件(Strust的action层) @Repository用于标注数据库访问组件,即DAO组件 @Component泛指组件,当不好归类时用其进行标注 配置文件: <?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--spring 扫描的范围--> <!--该配置项包含很多注解处理器, 其中就包括<context:annotation-config/>配置项的处理器, --> <context:component-scan base-package="com.lid"/> </beans> 类: //@Service----不指定名称,则是类名称第一个字符小写后即(personServiceBean) @Service("personService")//因为默认是单例模式 @Scope("prototype")//改成非单利模式 public class PersonServiceBean { private PersonDao personDao; private String name="xxx"; public PersonServiceBean(PersonDao personDao,String name){ this.personDao=personDao; this.name=name; } @PostConstruct public void init(){ System.out.println("---类被初始化之后就执行该方法"); } @PreDestroy public void destory(){ System.out.println("---类被销毁之前就执行该方法"); } public void save(){ System.out.println("-----spring test----"); } }