Spring Bean 生命周期2
2016-03-04 09:48
471 查看
在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一个实例,而不是每次都产生一个新的对象使用Singleton模式产生单一实例,对单线程的程序说并不会有什么问题,但对于多线程的程序,就必须注意安全(Thread-safe)的议题,防止多个线程同时存取共享资源所引发的数据不同步问题。
然而在spring中 可以设定每次从BeanFactory或ApplicationContext指定别名并取得Bean时都产生一个新的实例:例如:
在spring中,singleton属性默认是true,只有设定为false,则每次指定别名取得的Bean时都会产生一个新的实例
一个Bean从创建到销毁,如果是用BeanFactory来生成,管理Bean的话,会经历几个执行阶段(如图1.1):
1:Bean的建立:
容器寻找Bean的定义信息并将其实例化。
2:属性注入:
使用依赖注入,Spring按照Bean定义信息配置Bean所有属性
3:BeanNameAware的setBeanName():
如果Bean类有实现org.springframework.beans.BeanNameAware接口,工厂调用Bean的setBeanName()方法传递Bean的ID。
4:BeanFactoryAware的setBeanFactory():
如果Bean类有实现org.springframework.beans.factory.BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身。
5:BeanPostProcessors的ProcessBeforeInitialization()
如果有org.springframework.beans.factory.config.BeanPostProcessors和Bean关联,那么其postProcessBeforeInitialization()方法将被将被调用。
6:initializingBean的afterPropertiesSet():
如果Bean类已实现org.springframework.beans.factory.InitializingBean接口,则执行他的afterProPertiesSet()方法
7:Bean定义文件中定义init-method:
可以在Bean定义文件中使用"init-method"属性设定方法名称例如:
如果有以上设置的话,则执行到这个阶段,就会执行initBean()方法
8:BeanPostProcessors的ProcessaAfterInitialization()
如果有任何的BeanPostProcessors实例与Bean实例关联,则执行BeanPostProcessors实例的ProcessaAfterInitialization()方法
此时,Bean已经可以被应用系统使用,并且将保留在BeanFactory中知道它不在被使用。有两种方法可以将其从BeanFactory中删除掉(如图1.2):
1:DisposableBean的destroy()
在容器关闭时,如果Bean类有实现org.springframework.beans.factory.DisposableBean接口,则执行他的destroy()方法
2:Bean定义文件中定义destroy-method
在容器关闭时,可以在Bean定义文件中使用"destroy-method"属性设定方法名称,例如:
如果有以上设定的话,则进行至这个阶段时,就会执行destroy()方法,如果是使用ApplicationContext来生成并管理Bean的话则稍有不同,使用ApplicationContext来生成及管理Bean实例的话,在执行BeanFactoryAware的setBeanFactory()阶段后,若Bean类上有实现org.springframework.context.ApplicationContextAware接口,则执行其setApplicationContext()方法,接着才执行BeanPostProcessors的ProcessBeforeInitialization()及之后的流程。
第二种是:通过 在xml中定义init-method 和 destory-method方法
第三种是: 通过bean实现InitializingBean和 DisposableBean接口
分别介绍下:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
1:定义相关的实现类:
2:定义相关的配置文件:
其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;
测试类:
测试结果:
I'm init method using @PostConstrut....123
I'm destory method using @PreDestroy.....123
其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor类来告诉Spring容器采用的常用注解配置的方式:
只需要修改配置文件为:
同样可以得到以上测试的输出结果。
第二种、init-method 和 destory-method
在xml中配置 init-method和 destory-method方法
只是定义spring 容器在初始化bean 和容器销毁之前的所做的操作
基于xml的配置只是一种方式:
定义PersonService类:
相应的测试类:
测试结果:
init
hello spring
hello spring
cleanUp
可以看出 init 方法和 clean up方法都已经执行了。
context.registerShutdownHook(); 是一个钩子方法,当jvm关闭退出的时候会调用这个钩子方法,在设计模式之 模板模式中 通过在抽象类中定义这样的钩子方法由实现类进行实现,这里的实现类是AbstractApplicationContext,这是spring 容器优雅关闭的方法。
第三种是: 通过bean实现InitializingBean和 DisposableBean接口
1:定义相应类实现InitializingBean ,DisposableBean 接口
2:定义相应的配置文件:
4:输出测试结果:
然而在spring中 可以设定每次从BeanFactory或ApplicationContext指定别名并取得Bean时都产生一个新的实例:例如:
在spring中,singleton属性默认是true,只有设定为false,则每次指定别名取得的Bean时都会产生一个新的实例
一个Bean从创建到销毁,如果是用BeanFactory来生成,管理Bean的话,会经历几个执行阶段(如图1.1):
1:Bean的建立:
容器寻找Bean的定义信息并将其实例化。
2:属性注入:
使用依赖注入,Spring按照Bean定义信息配置Bean所有属性
3:BeanNameAware的setBeanName():
如果Bean类有实现org.springframework.beans.BeanNameAware接口,工厂调用Bean的setBeanName()方法传递Bean的ID。
4:BeanFactoryAware的setBeanFactory():
如果Bean类有实现org.springframework.beans.factory.BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身。
5:BeanPostProcessors的ProcessBeforeInitialization()
如果有org.springframework.beans.factory.config.BeanPostProcessors和Bean关联,那么其postProcessBeforeInitialization()方法将被将被调用。
6:initializingBean的afterPropertiesSet():
如果Bean类已实现org.springframework.beans.factory.InitializingBean接口,则执行他的afterProPertiesSet()方法
7:Bean定义文件中定义init-method:
可以在Bean定义文件中使用"init-method"属性设定方法名称例如:
如果有以上设置的话,则执行到这个阶段,就会执行initBean()方法
8:BeanPostProcessors的ProcessaAfterInitialization()
如果有任何的BeanPostProcessors实例与Bean实例关联,则执行BeanPostProcessors实例的ProcessaAfterInitialization()方法
此时,Bean已经可以被应用系统使用,并且将保留在BeanFactory中知道它不在被使用。有两种方法可以将其从BeanFactory中删除掉(如图1.2):
1:DisposableBean的destroy()
在容器关闭时,如果Bean类有实现org.springframework.beans.factory.DisposableBean接口,则执行他的destroy()方法
2:Bean定义文件中定义destroy-method
在容器关闭时,可以在Bean定义文件中使用"destroy-method"属性设定方法名称,例如:
如果有以上设定的话,则进行至这个阶段时,就会执行destroy()方法,如果是使用ApplicationContext来生成并管理Bean的话则稍有不同,使用ApplicationContext来生成及管理Bean实例的话,在执行BeanFactoryAware的setBeanFactory()阶段后,若Bean类上有实现org.springframework.context.ApplicationContextAware接口,则执行其setApplicationContext()方法,接着才执行BeanPostProcessors的ProcessBeforeInitialization()及之后的流程。
一、关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作第二种是:通过 在xml中定义init-method 和 destory-method方法
第三种是: 通过bean实现InitializingBean和 DisposableBean接口
分别介绍下:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
1:定义相关的实现类:
package com.myapp.core.annotation.init; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class PersonService { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @PostConstruct public void init(){ System.out.println("I'm init method using @PostConstrut...."+message); } @PreDestroy public void dostory(){ System.out.println("I'm destory method using @PreDestroy....."+message); } }
2:定义相关的配置文件:
<?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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> --> <context:annotation-config /> <bean id="personService" class="com.myapp.core.annotation.init.PersonService"> <property name="message" value="123"></property> </bean> </beans>
其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;
测试类:
package com.myapp.core.annotation.init; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("resource/annotation.xml"); PersonService personService = (PersonService)context.getBean("personService"); personService.dostory(); //是不是用关闭容器模拟更有说服力context.registerShutdownHook();
} }
测试结果:
I'm init method using @PostConstrut....123
I'm destory method using @PreDestroy.....123
其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor类来告诉Spring容器采用的常用注解配置的方式:
只需要修改配置文件为:
<?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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> --> <!-- <context:annotation-config /> --> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <bean id="personService" class="com.myapp.core.annotation.init.PersonService"> <property name="message" value="123"></property> </bean> </beans>
同样可以得到以上测试的输出结果。
第二种、init-method 和 destory-method
在xml中配置 init-method和 destory-method方法
只是定义spring 容器在初始化bean 和容器销毁之前的所做的操作
基于xml的配置只是一种方式:
<bean id="personService" class="com.myapp.core.beanscope.PersonService" scope="singleton" init-method="init" destroy-method="cleanUp"> </bean>
定义PersonService类:
package com.myapp.core.beanscope; public class PersonService { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void init(){ System.out.println("init"); } // how validate the destory method is a question public void cleanUp(){ System.out.println("cleanUp"); } }
相应的测试类:
package com.myapp.core.beanscope; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { AbstractApplicationContext context =new ClassPathXmlApplicationContext("SpringBeans.xml"); PersonService person = (PersonService)context.getBean("personService"); person.setMessage("hello spring"); PersonService person_new = (PersonService)context.getBean("personService"); System.out.println(person.getMessage()); System.out.println(person_new.getMessage()); context.registerShutdownHook(); } }
测试结果:
init
hello spring
hello spring
cleanUp
可以看出 init 方法和 clean up方法都已经执行了。
context.registerShutdownHook(); 是一个钩子方法,当jvm关闭退出的时候会调用这个钩子方法,在设计模式之 模板模式中 通过在抽象类中定义这样的钩子方法由实现类进行实现,这里的实现类是AbstractApplicationContext,这是spring 容器优雅关闭的方法。
第三种是: 通过bean实现InitializingBean和 DisposableBean接口
1:定义相应类实现InitializingBean ,DisposableBean 接口
package com.myapp.core.annotation.init; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class PersonService implements InitializingBean,DisposableBean{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @Override public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("I'm init method using implements InitializingBean interface...."+message); } @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("I'm init method using implements DisposableBean interface...."+message); } }
2:定义相应的配置文件:
<?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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> --> <!-- <context:annotation-config /> --> <!-- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <bean id="personService" class="com.myapp.core.annotation.init.PersonService"> <property name="message" value="123"></property> </bean> --> <bean id="personService" class="com.myapp.core.annotation.init.PersonService"> <property name="message" value="123"></property> </bean> </beans>
3:测试类:
package com.myapp.core.annotation.init; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("resource/annotation.xml"); PersonService personService = (PersonService)context.getBean("personService"); context.registerShutdownHook(); } }
4:输出测试结果:
I'm init method using implements DisposableBean interface....123 三月 16, 2013 5:06:34 下午 org.springframework.context.support.AbstractApplicationContext doClose INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@205756: startup date [Sat Mar 16 17:06:30 CST 2013]; root of context hierarchy I'm init method using implements InitializingBean interface....123
相关文章推荐
- Eclipse配置信息
- JavaBean
- java中dynamic web project与web project 的区别
- 深入分析Java ClassLoader原理
- 简单模拟spring装载注入bean原理
- 【Javaweb】Eclipse for JavaEE新建的Web工程自动生成web.xml
- Spring MVC之@RequestParam @RequestBody @RequestHeader 等详解
- Java读取UTF-8格式txt文件第一行出现乱码——问号“?”及解决
- Pojo和JavaBean的区别
- Java运算符
- JNI中C调用Java方法
- LintCode 最大数
- JavaBean,POJO,VO,DTO的区别和联系
- Java--容器(Collection)(一)
- Leetcode:198. House Robber(JAVA)
- JAVA SDutOJ 题目收录
- eclipse 出包 is not a sibling in the same 问题修复
- Java 服务器端修改apk并重新打包签名
- Android studio导入eclipse的项目时,l解决libpng warning: iCCP: Not recognizing known sRGB profile that has be
- Java 学习的几个基础实验(Learn by doing)