1. 程式人生 > >Spring原始碼解析(七)——生命週期——BeanPostProcessor

Spring原始碼解析(七)——生命週期——BeanPostProcessor

https://blog.csdn.net/u011734144/article/details/72600932

http://www.cnblogs.com/lucas2/p/9430169.html

 

BeanPostProcessor:bean的後置處理器。在bean初始化前後進行一些處理工作。

public interface BeanPostProcessor {

	/**
	 * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
	 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
	 * or a custom init-method). The bean will already be populated with property values.
	 * The returned bean instance may be a wrapper around the original.
	 * @param bean the new bean instance
	 * @param beanName the name of the bean
	 * @return the bean instance to use, either the original or a wrapped one;
	 * if {@code null}, no subsequent BeanPostProcessors will be invoked
	 * @throws org.springframework.beans.BeansException in case of errors
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
	 * 初始化之前工作
	 */
	Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;

	/**
	 * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
	 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
	 * or a custom init-method). The bean will already be populated with property values.
	 * The returned bean instance may be a wrapper around the original.
	 * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
	 * instance and the objects created by the FactoryBean (as of Spring 2.0). The
	 * post-processor can decide whether to apply to either the FactoryBean or created
	 * objects or both through corresponding {@code bean instanceof FactoryBean} checks.
	 * <p>This callback will also be invoked after a short-circuiting triggered by a
	 * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
	 * in contrast to all other BeanPostProcessor callbacks.
	 * @param bean the new bean instance
	 * @param beanName the name of the bean
	 * @return the bean instance to use, either the original or a wrapped one;
	 * if {@code null}, no subsequent BeanPostProcessors will be invoked
	 * @throws org.springframework.beans.BeansException in case of errors
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
	 * @see org.springframework.beans.factory.FactoryBean
	 * 初始化之後工作
	 */
	Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

}

例子

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * 後置處理器:初始化前後進行處理工作
 * 將後置處理器加入到容器中
 * @author lfy
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
        return bean;
    }
}
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

//@Component
public class Car implements InitializingBean, DisposableBean {

    private String name;

    public Car(String name) {
        this.name = name;
        System.out.println("car constructor...");
    }

    public void initMethod() {
        System.out.println("car name: " + name);
        System.out.println("car ... initMethod...");
    }

    public void destoryMethod() {
        System.out.println("car ... destoryMethod...");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("car ...DisposableBean... destory");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("car...InitializingBean...afterPropertiesSet");
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println("car...postConstruct");
    }

    @PreDestroy
    public void preDestory() {
        System.out.println("car...preDestory");
    }
}
import com.atguigu.bean.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * bean的生命週期:
 * 		bean建立---初始化----銷燬的過程
 * 容器管理bean的生命週期;
 * 我們可以自定義初始化和銷燬方法;容器在bean進行到當前生命週期的時候來呼叫我們自定義的初始化和銷燬方法
 *
 * 構造(物件建立)
 * 		單例項:在容器啟動的時候建立物件
 * 		多例項:在每次獲取的時候建立物件\
 *
 * BeanPostProcessor.postProcessBeforeInitialization
 * 初始化:
 * 		物件建立完成,並賦值好,呼叫初始化方法。。。
 * BeanPostProcessor.postProcessAfterInitialization
 * 銷燬:
 * 		單例項:容器關閉的時候
 * 		多例項:容器不會管理這個bean;容器不會呼叫銷燬方法;
 *
 *
 * 遍歷得到容器中所有的BeanPostProcessor;挨個執行beforeInitialization,
 * 一但返回null,跳出for迴圈,不會執行後面的BeanPostProcessor.postProcessorsBeforeInitialization
 *
 * BeanPostProcessor原理
 * populateBean(beanName, mbd, instanceWrapper);給bean進行屬性賦值
 * initializeBean
 * {
 * applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
 * invokeInitMethods(beanName, wrappedBean, mbd);執行自定義初始化
 * applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
 *}
 *
 *
 *
 * 1)、指定初始化和銷燬方法;
 * 		通過@Bean指定init-method和destroy-method;
 * 2)、通過讓Bean實現InitializingBean(定義初始化邏輯),
 * 				DisposableBean(定義銷燬邏輯);
 * 3)、可以使用JSR250;
 * 		@PostConstruct:在bean建立完成並且屬性賦值完成;來執行初始化方法
 * 		@PreDestroy:在容器銷燬bean之前通知我們進行清理工作
 * 4)、BeanPostProcessor【interface】:bean的後置處理器;
 * 		在bean初始化前後進行一些處理工作;
 * 		postProcessBeforeInitialization:在初始化之前工作
 * 		postProcessAfterInitialization:在初始化之後工作
 *
 * Spring底層對 BeanPostProcessor 的使用;
 * 		bean賦值,注入其他元件,@Autowired,生命週期註解功能,@Async,xxx BeanPostProcessor;
 *
 * @author lfy
 *
 */
@ComponentScan("com.atguigu.bean")
@Configuration
public class MainConfigOfLifeCycle {

    @Bean(initMethod="initMethod",destroyMethod="destoryMethod")
    public Car car(){
        return new Car("audi");
    }
}
import com.atguigu.config.MainConfigOfLifeCycle;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class IOCTest_LifeCycle {

    @Test
    public void test01(){
        //1、建立ioc容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
        System.out.println("容器建立完畢");

        //關閉容器
        applicationContext.close();
    }
}

執行結果

從結果可以看出,spring管理的bean,會先執行postProcessBeforeInitialization,然後再執行初始化方法,如:@PostConstruct > InitializingBean > init-method

初始化:物件建立完成,並賦值好,呼叫初始化方法(@PostConstruct > InitializingBean > init-method)

初始化之前,呼叫BeanPostProcessor.postProcessBeforeInitialization

初始化之後,呼叫BeanPostProcessor.postProcessAfterInitialization

 

跟蹤原始碼:

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class)=>

refresh();=>
finishBeanFactoryInitialization(beanFactory);=>

beanFactory.setConversionService(
                    beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));=>

doGetBean(name, requiredType, null, false);=>

sharedInstance = getSingleton=>

return createBean(beanName, mbd, args);=>

Object beanInstance = doCreateBean(beanName, mbdToUse, args);=>

populateBean先為屬性賦值,然後initializeBean

applyBeanPostProcessorsBeforeInitialization方法,遍歷得到容器中所有的BeanPostProcessor,挨個執行beforeInitialization。一旦返回null,跳出for迴圈,不會執行後面的beanProcessor.postProcessAfterInitialization。

小總結

 * BeanPostProcessor原理
 * populateBean(beanName, mbd, instanceWrapper);給bean進行屬性賦值
 * initializeBean
 * {
 * applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
 * invokeInitMethods(beanName, wrappedBean, mbd);執行自定義初始化
 * applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
 *}