當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
【Spring注解系列11】Spring后置处理器BeanPostProcessor用法与原理
生活随笔
收集整理的這篇文章主要介紹了
【Spring注解系列11】Spring后置处理器BeanPostProcessor用法与原理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.BeanPostProcessor原理
先說,bean的后置處理器BeanPostProcessor接口中兩個方法:
- postProcessBeforeInitialization:在初始化之前工作
- postProcessAfterInitialization:在初始化之后工作
?
BeanPostProcessor原理
- populateBean(beanName, mbd, instanceWrapper);//給bean進行屬性賦值
- 調用initializeBean方法,執行后置處理器和指定的初始化方法。(詳細過程見下面源碼)
- 后置處理器BeanPostProcessor執行過程是,遍歷得到容器中所有的BeanPostProcessor;挨個執行beforeInitialization,一但返回null,跳出for循環,不會執行后面的BeanPostProcessor.postProcessorsBeforeInitialization
?
核心執行方法initializeBean:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean
initializeBean
{
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
invokeInitMethods(beanName, wrappedBean, mbd);執行自定義初始化
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
/*** Initialize the given bean instance, applying factory callbacks* as well as init methods and bean post processors.* <p>Called from {@link #createBean} for traditionally defined beans,* and from {@link #initializeBean} for existing bean instances.* @param beanName the bean name in the factory (for debugging purposes)* @param bean the new bean instance we may need to initialize* @param mbd the bean definition that the bean was created with* (can also be {@code null}, if given an existing bean instance)* @return the initialized bean instance (potentially wrapped)* @see BeanNameAware* @see BeanClassLoaderAware* @see BeanFactoryAware* @see #applyBeanPostProcessorsBeforeInitialization* @see #invokeInitMethods* @see #applyBeanPostProcessorsAfterInitialization*/protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {if (System.getSecurityManager() != null) {AccessController.doPrivileged((PrivilegedAction<Object>) () -> {invokeAwareMethods(beanName, bean);return null;}, getAccessControlContext());}else {//調用實現XXAware接口的方法invokeAwareMethods(beanName, bean);}Object wrappedBean = bean;if (mbd == null || !mbd.isSynthetic()) {//后置處理器--前置方法wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);}try {//調用@bean或者<bean>標簽中指定的初始化方法invokeInitMethods(beanName, wrappedBean, mbd);}catch (Throwable ex) {throw new BeanCreationException((mbd != null ? mbd.getResourceDescription() : null),beanName, "Invocation of init method failed", ex);}if (mbd == null || !mbd.isSynthetic()) {//后置處理器--后置方法wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);}return wrappedBean;}//后置處理器--前置方法@Overridepublic Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)throws BeansException {Object result = existingBean;for (BeanPostProcessor processor : getBeanPostProcessors()) {Object current = processor.postProcessBeforeInitialization(result, beanName);if (current == null) {return result;}result = current;}return result;}//后置處理器--后置方法 處理方式都是會遍歷所有的后置處理器,調用前置或后置方法@Overridepublic Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)throws BeansException {Object result = existingBean;for (BeanPostProcessor processor : getBeanPostProcessors()) {Object current = processor.postProcessAfterInitialization(result, beanName);if (current == null) {return result;}result = current;}return result;}2.BeanPostProcessor 后置處理器在Spring底層中有那些應用
Spring底層對 BeanPostProcessor 的使用;
bean賦值,注入其他組件,@Autowired,生命周期注解功能,@Async,xxx BeanPostProcessor;
?
3.BeanPostProcessor 使用
?
public class BeanLife {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "BeanLife{" +"name='" + name + '\'' +'}';}public BeanLife() {System.out.println("構造方法:BeanLife--->construct...");}}/*** @author tuchuanbiao* @Date 2019/4/3 19:58** 后置處理器:初始化前后進行處理工作* 將后置處理器加入到容器中** 這里采用@Bean方式注入,也可以直接使用@Component*/ //@Component //一定要將后置處理器注入到容器中 public class MyBeanPostProcessor implements BeanPostProcessor {@Nullable@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("bean后置處理器:MyBeanPostProcessor......postProcessBeforeInitialization");return bean;}@Nullable@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("bean后置處理器:MyBeanPostProcessor......postProcessAfterInitialization");return bean;} }@Configuration public class BeanLifeCycleConfig {@Bean(value = "beanLife")public BeanLife life() {BeanLife beanLife = new BeanLife();beanLife.setName("張三");System.out.println(beanLife);return beanLife;}@Beanpublic MyBeanPostProcessor myBeanPostProcessor(){return new MyBeanPostProcessor();} }//測試類public class BeanLifeCycleTest {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanLifeCycleConfig.class);System.out.println("applicationContext ..... 初始化結束");System.out.println("applicationContext ..... 準備關閉");applicationContext.close();System.out.println("applicationContext ..... 已關閉");}}運行結果: 構造方法:BeanLife--->construct... BeanLife{name='張三'} bean后置處理器:MyBeanPostProcessor......postProcessBeforeInitialization bean后置處理器:MyBeanPostProcessor......postProcessAfterInitialization applicationContext ..... 初始化結束 applicationContext ..... 準備關閉 applicationContext ..... 已關閉?
總結
以上是生活随笔為你收集整理的【Spring注解系列11】Spring后置处理器BeanPostProcessor用法与原理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Spring注解系列10】Spring
- 下一篇: 【Spring注解系列12】@Value