Bean生命周期管理:
Bean的完整生命周期经历了各种方法调用,这些方法可以划分为以下几类:
1、Bean类自身的方法 : 这个包括了Bean本身调用的方法和通过配置文件中<bean>的init-method和destroy-method指定的方法
2、Bean级生命周期接口方法 : 这个包括了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean这些接口的方法 ;
60 // 这是BeanFactoryAware接口方法
61 @Override
62 public void setBeanFactory(BeanFactory arg0) throws BeansException {
63 System.out
64 .println("【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()");
65 this.beanFactory = arg0;
66 }
67
68 // 这是BeanNameAware接口方法
69 @Override
70 public void setBeanName(String arg0) {
71 System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()");
72 this.beanName = arg0;
73 }
74
75 // 这是InitializingBean接口方法
76 @Override
77 public void afterPropertiesSet() throws Exception {
78 System.out
79 .println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");
80 }
81
82 // 这是DiposibleBean接口方法
83 @Override
84 public void destroy() throws Exception {
85 System.out.println("【DiposibleBean接口】调用DiposibleBean.destory()");
86 }
87
88 // 通过<bean>的init-method属性指定的初始化方法
89 public void myInit() {
90 System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
91 }
92
93 // 通过<bean>的destroy-method属性指定的初始化方法
94 public void myDestory() {
95 System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
96 }
3、容器级生命周期接口方法 : 这个包括了InstantiationAwareBeanPostProcessor 和 BeanPostProcessor 这两个接口实现,一般称它们的实现类为“后处理器”。
4、工厂后处理器接口方法 : 这个包括了AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer等等非常有用的工厂后处理器 接口的方法。工厂后处理器也是容器级的。在应用上下文装配配置文件之后立即调用。