Spring注解应用篇--IOC容器Bean生命周期
这是Spring注解专题系类文章,本系类文章适合Spring入门者或者原理入门者,小编会在本系类文章下进行企业级应用实战讲解以及spring源码跟进。
本文来自公众号:B一下爪哇
环境准备
- 编译器IDEA
- maven依赖spring-context version:4.3.12.RELEASE
- maven依赖junit version:4.11
Bean注解来指定bean初始化和销毁方法
前面一章提过,在配置类中通过@Bean来将组件注入到容器中,在容器中,Bean的生命周期大抵上可以分为创建--初始化--销毁的过程,容器管理着组件的全部生命周期。Bean注解源码里面包含initMethod和destroyMethod两个属性,可以分别来自定义bean的初始化方法和销毁方法。 自定义格式:
@Bean(initMethod=,destoryMethod=)复制代码
测试: 先在Bean中自定义初始化和销毁方法:
public class ExampleBean { private String name; private int age;.....public void init(){ System.out.println("ExampleB init..."); } public void destory(){ System.out.println("Example destory..."); }}复制代码
配置类:
@Configurationpublic class BaseConfig { @Bean(value="beanIdDefinition",initMethod = "init",destroyMethod = "destory") public ExampleBean exampleBean(){ return new ExampleBean(); }}复制代码
测试类:
@Test public void shouldAnswerWithTrue() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(BaseConfig.class); System.out.println("IOC容器创建完成..."); String[] beanDefinitionNames = ctx.getBeanDefinitionNames(); ctx.close(); }复制代码
output:
exampleBean constructor......ExampleB init...IOC容器创建完成...Example destory...复制代码
由此可知,在容器初始化过程中已经完成对bean的初始化工作,并且在容器关闭途中,调用bean的销毁方法。(下一章分析容器初始化大致做了哪一些工作)
InitializingBean和DisposableBean接口指定Bean初始化和销毁方法
InitializingBean的afterPropertiesSet在设置提供Bean的属性值后由BeanFactory调用进行方法调用。 DisposableBean的destory在Bean单例被破坏时由BeanFactory进行方法调用。 定义Bean并实现这两个接口
public class LifeCycleBean implements InitializingBean, DisposableBean{ @Override public void afterPropertiesSet() throws Exception { System.out.println("LifeCycleBean afterPropertieSet...."); } @Override public void destroy() throws Exception { System.out.println("LifeCycleBean destory...."); }}复制代码
output:
LifeCycleBean afterPropertieSet....IOC容器创建完成...LifeCycleBean destory....复制代码
PostConstruct & PreDestory注解
JSR是Java Specification Requests的缩写,意思是Java 规范提案。 JSR250已经添加至Java SE6中,常用的大概就是PostConstruct初始化、PreDestory销毁、Resource资源等。 Spring支持JSR250的注解。PostConstruct在bean创建完成并且属性赋值完成;来执行初始化方法;PreDestory在容器销毁bean之前回调通知;Resource支持bean自动装配,类似Autowired。 使用方式,在Bean中直接添加:
@PostConstruct public void initPost(){ System.out.println("ExampleBean JSR250 PostConstruct..."); } @PreDestroy public void destoryPre(){ System.out.println("ExampleBean JSR250 PreDestory..."); }复制代码
测试output:
exampleBean constructor......ExampleBean JSR250 PostConstruct...IOC容器创建完成...ExampleBean JSR250 PreDestory...复制代码
BeanPostProcessor后置处理器
BeanPostProcessor【接口】:bean后置处理器在Bean的生命周期起到一个承上启下的作用,在Bean初始化前后进行处理工作。
- PostProcessorBeforeInitialization方法:在初始化方法调用之前调用;
- postProcessorBefore方法:在bean初始化方法调用之后工作 两个方法都包含2个相同的参数: @param Object o:Bean实例 @param String s:beanName
定义一个Bean实现后置处理器接口:
public class BeanPostProcessorDefinition implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object o, String s) throws BeansException { System.out.println("postProcessBeforeInitialization -->"+s+" = "+o); return o; } @Override public Object postProcessAfterInitialization(Object o, String s) throws BeansException { System.out.println("postProcessorAfterInitialization -->"+s+"="+o); return o; }}复制代码
以容器中存在的id为exampleBean的bean为例:查看output:
//部分容器初始化的一些beanexampleBean constructor......postProcessBeforeInitialization -->exampleBean = ExampleBean{name='null', age=0}ExampleBean JSR250 PostConstruct...postProcessorAfterInitialization -->exampleBean=ExampleBean{name='null', age=0}IOC容器创建完成...复制代码
由此不难得出,在容器初始化过程中,大致依次完成bean的创建,实例化,属性赋值,执行bean后置处理器初始化之前的方法,执行初始化方法,执行bean后置处理器初始化之后的方法。