1、問(wèn)題描述
某些spring 托管的bean,被標(biāo)注Autowired注解的屬性,沒(méi)有自動(dòng)注入對(duì)象。比如下面這個(gè)類(lèi)。
/**
* Created by zhengwei on 2017/6/21.
*/
@Component
public classGetProductByProductIdRequestHandlerextendsPcRequestHandler {
@Autowired
privateProductQueryManagerproductQueryManager;
@Override
protectedGetProductByProductIdResponsedoHandle(GetProductByProductIdRequest request) {
GetProductByProductIdResponse response =newGetProductByProductIdResponse();
ProductDO product;
try{
product =this.productQueryManager.getProductBaseInfoById(request.getProductId());
}catch(Exception e) {
logger.error("getProductBaseInfoById error, productId="+ request.getProductId(),e);
response.setSuccess(false);
response.setResultCode(BizErrorCode.COMMON_ERROR.getErrorCode());
response.setResultMsg(BizErrorCode.COMMON_ERROR.getErrorMsg());
returnresponse;
}
response.setProduct(product);
response.setSuccess(true);
returnresponse;
}
@Override
publicClasssupportRequest() {
returnGetProductByProductIdRequest.class;
}
}
運(yùn)行的過(guò)程中,上圖GetProductByProductIdRequestHandler中的屬性productQueryManager為null,導(dǎo)致空指針異常。
2、問(wèn)題產(chǎn)生原因
項(xiàng)目啟用了配置中心,通過(guò)PropertyPlaceholderConfigurer實(shí)現(xiàn),因此它是個(gè)BeanFactoryPostProcessor,而此bean在初始化spring時(shí),會(huì)去配置中心獲取配置項(xiàng)到本地內(nèi)存,其中該bean初始化過(guò)程中,有這么一段代碼,如下圖:
private void initListener(PropertiesConfiguration config) {
Map listeners = applicationContext.getBeansOfType(MessageConfCenterListener.class);
if (listeners == null) {
listeners = new HashMap();
}
// add default contextPropertiesListener
listeners.put("contextPropertiesListener", new MessageConfCenterListener() {
@Override
public void notifyEvent(ConfigurationEvent event) {
switch (event.getType()) {
case ADD:
case UPDATE:
contextProperties.put(event.getPropertyName(), event.getPropertyValue());
break;
case CLEAR:
contextProperties.remove(event.getPropertyName());
}
}
});
config.addConfigurationListener(new MessageConfigurationListener(listeners.values()));
}
可以看到其中第2行代碼是,
Map listeners = applicationContext.getBeansOfType(MessageConfCenterListener.class);
也就是從applicationContext獲取MessageConfCenterListener,getBeansOfType此方法是根據(jù)name遍歷整個(gè)bean factory里的bean,如果是FactoryBean的話(huà),會(huì)先創(chuàng)建此bean,問(wèn)題就出在這里了。這里需要看看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.
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();
}
}
}
1、prepareBeanFactory(beanFactory)方法,表示準(zhǔn)備動(dòng)作,初始化一些默認(rèn)的配置。
2、invokeBeanFactoryPostProcessors(beanFactory),執(zhí)行BeanFactoryPostProcessor,而我們的配置中心是通過(guò)PropertyPlaceholderConfigurer來(lái)實(shí)現(xiàn)的,而PropertyPlaceholderConfigurer是一個(gè)BeanFactoryPostProcessor,因此這個(gè)時(shí)候會(huì)被執(zhí)行。
3、registerBeanPostProcessors(beanFactory),注冊(cè)BeanPostProcessor,這個(gè)BeanPostProcessor干啥的呢,spring的Autowired注解就是通過(guò)AutowiredAnnotationBeanPostProcessor來(lái)實(shí)現(xiàn)的。
因此當(dāng)執(zhí)行BeanFactoryPostProcessor的時(shí)候,spring還沒(méi)有往容器里注冊(cè)AutowiredAnnotationBeanPostProcessor,問(wèn)題就這么發(fā)生了。