spring bean初始化擴展之BeanPostProcessor源碼解析

BeanPostProcessor介紹

上一篇博客spring bean初始化擴展之Aware源碼解析,介紹了Aware的用處,其作爲spring的一個擴展點。這篇博客將介紹spring的另一個擴展點BeanPostProcessor。先看下BeanPostProcessor源碼

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.
	 * <p>The default implementation returns the given {@code bean} as-is.
	 * @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
	 */
	@Nullable
	default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	/**
	 * 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.
	 * <p>The default implementation returns the given {@code bean} as-is.
	 * @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
	 */
	@Nullable
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

}

可以看到BeanPostProcessor接口定義了兩個方法,一個是before,一個是after,入參都是bean和beanName。

BeanPostProcessor的作用執行時機

繼續通過AbstractAutowireCapableBeanFactory中初始化bean的源碼中,瞭解一下BeanPostProcessor的作用執行時機。

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 {
			//調用調用此方法進行部分Aware接口的回調
			invokeAwareMethods(beanName, bean);
		}

		Object wrappedBean = bean;
		if (mbd == null || !mbd.isSynthetic()) {
			//調用後置處理器的前置方法
			wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
		}

		try {
			//調用初始化方法
			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;
	}

可以看到在bean初始化的過程中,在調用bean自定義初始化方法的前後,分別執行applyBeanPostProcessorsBeforeInitialization 和 applyBeanPostProcessorsAfterInitialization 兩個方法。我們看一下這兩個方法。

@Override
	public 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;
	}

	@Override
	public 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;
	}

可以看到這兩個方法都是分別拿到所有的BeanPostProcessor,然後遍歷執行相應的before和after方法。因此可以總結,BeanPostProcessor的作用其實就是在普通bean初始化時,調用bean的自定義或非自定義初始化方法的前後進行相應的操作,以達到對bean進行修改擴展。

BeanPostProcessor的註冊

通過以上的介紹,我們知道BeanPostProcessor的作用,那麼這些BeanPostProcessor是什麼時候被註冊上去的呢?上面源碼有通過getBeanPostProcessors方法獲取所有的BeanPostProcessor。這裏getBeanPostProcessors其實是調用AbstractAutowireCapableBeanFactory的父類AbstractBeanFactory的getBeanPostProcessors方法

	/**
	 * Return the list of BeanPostProcessors that will get applied
	 * to beans created with this factory.
	 */
	public List<BeanPostProcessor> getBeanPostProcessors() {
		return this.beanPostProcessors;
	}

可以看到這裏直接返回一個list。而這個list是AbstractBeanFactory的一個成員變量,也就是肯定有入口add相應的beanPostProcessor進入這個list。而這個地方就是AbstractBeanFactory的addBeanPostProcessor方法。

@Override
	public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
		Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
		// Remove from old position, if any
		this.beanPostProcessors.remove(beanPostProcessor);
		// Track whether it is instantiation/destruction aware
		if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
			this.hasInstantiationAwareBeanPostProcessors = true;
		}
		if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
			this.hasDestructionAwareBeanPostProcessors = true;
		}
		// Add to end of list
		this.beanPostProcessors.add(beanPostProcessor);
	}

那麼spring容器在什麼時候將beanPostProcessor添加進這個list呢。這裏有兩種方式,一種是普通的容器beanFactory的顯式add,另一種是ApplicationContext的自動註冊。

普通容器beanFactory的顯式調用添加BeanPostProcessor

普通的 BeanFactory 是不會自動註冊 BeanPostProcessor 的,需要我們手動調用 addBeanPostProcessor() 進行註冊,註冊後的 BeanPostProcessor 才能適用於所有該 BeanFactory 創建的 bean

ClassPathResource resource = new ClassPathResource("spring.xml");
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
//MyBeanPostProcessor是我自定義的一個BeanPostProcessor,後面示例會有源碼
MyBeanPostProcessor myBeanPostProcessor = new MyBeanPostProcessor();
//顯式調用,將myBeanPostProcessor添加到beanFactory中。
factory.addBeanPostProcessor(myBeanPostProcessor);

ApplicationContext 自動註冊BeanPostProcessor

ApplicationContext 容器可以自動檢測所有的 BeanPostProcessor 並自動完成註冊,而不需要手動添加註冊。
這個自動註冊過程其實在之前講解aop源碼的博客中有講過。細節分析可以閱讀這篇博客基於註解式的SpringAOP源碼解析-3。這裏再貼一下源碼。
AbstractApplicationContext的refresh方法

@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				//這一步就是註冊beanPostProcessor
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				initMessageSource();

				// Initialize event multicaster for this context.
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				onRefresh();

				// Check for listener beans and register them.
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				finishRefresh();
			}

			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				// Destroy already created singletons to avoid dangling resources.
				destroyBeans();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}

			finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				resetCommonCaches();
			}
		}
	}

可以看到容器在初始化的過程中,調用了registerBeanPostProcessors方法。這個方法就是自動註冊BeanPostProcessor的過程。這個過程可以前面說的博客中瞭解。

自定義BeanPostProcessor示例

MyBeanPostProcessor.java

自定義BeanPostProcessor

/**
 * 後置處理器:初始化前後進行處理
 *
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

	/**
	 *
	 * @param bean bean剛創建的實例
	 * @param beanName  實例的beanName
	 * @return
	 * @throws BeansException
	 */
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
		return bean;
	}

}

MainConfigOfLifeCycle.java

配置掃描讓自定義MyBeanPostProcessor被spring容器管理。

@ComponentScan("spring.annotation.pobean.lifecycle")
@Configuration
public class MainConfigOfLifeCycle {
}

IOCTest_LifeCycle.java

編寫單元測試,創建容器

public class IOCTest_LifeCycle {

    @Test
    public void test01(){
        //1、創建ioc容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
        System.out.println("容器創建完成...");
        //關閉容器
        applicationContext.close();
    }

}

運行結果

可以看到自定義的BeanPostProcessor已經生效
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章