Bean声明周期中的post-initialization和pre-destruction .
程序员文章站
2022-06-10 20:07:19
...
在Bean的声明周期中,有两个事件尤为重要:post-initialization和pre-destruction。
Spring提供了两种机制:interface-based和method-based,供bean签入上述事件。
所谓的post-initialization和pre-destruction,是指在bean的属性设置完毕执行的事件和在bean销毁之前执行的事件。
method-based机制:通过在BeanFactory中的配置,init-method和destory-method,通过设置这两个属性来指定要执行的回调方法。
interface-based机制:bean需要实现InitializatingBean接口和DisposableBean接口。这两个接口分别拥有afterPropertiesSet()方法和destroy()方法。这两个方法在bean初始化后和销毁前被执行。效果与method-based机制等同。
以下是网上流传一个例子,我在eclipse中运行了一下
package com.test; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class LifeCycle implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean { public LifeCycle() { System.out.println("Constructor executing"); } private String cycle; public void setCycle(String cycle) { System.out.println("Setter methods executing"); this.cycle = cycle; } @Override public void setBeanName(String beanName) { System.out.println("BeanNameAware.setBeanName(String beanName) executing"); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("BeanFactoryAware.setBeanFactory(BeanFactory beanFactory) executing"); } @PostConstruct public void beforeBeanConstructed() { System.out.println("BeanPostProcessor executing (before bean constructed)"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("InitializingBean.afterPropertiesSet() executing"); } public void initMethod() { System.out.println("InitMethod executing"); } @PreDestroy public void afterBeanConstructed() { System.out.println("BeanPostProcessor executing (after bean constructed)"); } @Override public void destroy() throws Exception { System.out.println("DisposableBean.destroy() executing"); } public void destroyMethod() { System.out.println("DestroyMethod executing"); } }
<?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-3.0.xsd"> <bean id="lifeCycle" class="com.test.LifeCycle" init-method="initMethod" destroy-method="destroyMethod"> <property name="cycle" value="Life Cycle"/> </bean> <bean id="beanPostProcessor" class="org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor"> <property name="initAnnotationType" value="javax.annotation.PostConstruct"/> <property name="destroyAnnotationType" value="javax.annotation.PreDestroy"/> </bean> </beans>
package com.test; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class LifeCycleDemo { public static void main(String[] args) { XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans-config.xml")); BeanPostProcessor bpp = (BeanPostProcessor)factory.getBean("beanPostProcessor"); factory.addBeanPostProcessor(bpp); LifeCycle lifeCycle = (LifeCycle)factory.getBean("lifeCycle"); factory.destroySingleton("lifeCycle"); } }
最后运行结果是: Constructor executing Setter methods executing BeanNameAware.setBeanName(String beanName) executing BeanFactoryAware.setBeanFactory(BeanFactory beanFactory) executing BeanPostProcessor executing (before bean constructed) InitializingBean.afterPropertiesSet() executing InitMethod executing BeanPostProcessor executing (after bean constructed) DisposableBean.destroy() executing DestroyMethod executing
注入Bean具体的实现:
具体的bean创建过程和依赖关系的注入在createBean中,这个方法在AbstractAutowireCapableBeanFactory中给出了实现:
protected Object createBean(String beanName, RootBeanDefinition mergedBeanDefinition, Object[] args) throws BeanCreationException { // Guarantee initialization of beans that the current one depends on. // 这里对取得当前bean的所有依赖bean,确定能够取得这些已经被确定的bean,如果没有被创建,那么这个createBean会被这些IOC // getbean时创建这些bean if (mergedBeanDefinition.getDependsOn() != null) { for (int i = 0; i < mergedBeanDefinition.getDependsOn().length; i++) { getBean(mergedBeanDefinition.getDependsOn()[i]); } } ........ // 这里是实例化bean对象的地方,注意这个BeanWrapper类,是对bean操作的主要封装类 if (instanceWrapper == null) { instanceWrapper = createBeanInstance(beanName, mergedBeanDefinition,args); } Object bean = instanceWrapper.getWrappedInstance(); ...... //这个populate方法,是对已经创建的bean实例进行依赖注入的地方,会使用到在loadBeanDefinition的时候得到的那些propertyValue来对bean进行注入。 if (continueWithPropertyPopulation) { populateBean(beanName, mergedBeanDefinition, instanceWrapper); } //这里完成客户自定义的对bean的一些初始化动作 Object originalBean = bean; bean = initializeBean(beanName, bean, mergedBeanDefinition); // Register bean as disposable, and also as dependent on specified "dependsOn"beans. registerDisposableBeanIfNecessary(beanName, originalBean,mergedBeanDefinition); return bean; } ......... }
上一篇: C# WPF如何使用多线程
下一篇: 青壮年可选拔罐保健