spring bean标注了Autowired注解的属性没有自动注入对象问题

1、问题描述

某些spring 托管的bean,被标注Autowired注解的属性,没有自动注入对象。比如下面这个类。


/**

* 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;

}

}

运行的过程中,上图GetProductByProductIdRequestHandler中的属性productQueryManager为null,导致空指针异常。

2、问题产生原因

项目启用了配置中心,通过PropertyPlaceholderConfigurer实现,因此它是个BeanFactoryPostProcessor,而此bean在初始化spring时,会去配置中心获取配置项到本地内存,其中该bean初始化过程中,有这么一段代码,如下图:


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此方法是根据name遍历整个bean factory里的bean,如果是FactoryBean的话,会先创建此bean,问题就出在这里了。这里需要看看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)方法,表示准备动作,初始化一些默认的配置。

2、invokeBeanFactoryPostProcessors(beanFactory),执行BeanFactoryPostProcessor,而我们的配置中心是通过PropertyPlaceholderConfigurer来实现的,而PropertyPlaceholderConfigurer是一个BeanFactoryPostProcessor,因此这个时候会被执行。

3、registerBeanPostProcessors(beanFactory),注册BeanPostProcessor,这个BeanPostProcessor干啥的呢,spring的Autowired注解就是通过AutowiredAnnotationBeanPostProcessor来实现的。

因此当执行BeanFactoryPostProcessor的时候,spring还没有往容器里注册AutowiredAnnotationBeanPostProcessor,问题就这么发生了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,969评论 6 342
  • 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 3.9 Annotation-base...
    SnailTyan阅读 1,194评论 0 3
  • 注意LifecycleProcessor接口继承了Lifcycle接口。同时,增加了2个方法,用于处理容器的ref...
    google666s阅读 1,124评论 0 51
  • 什么是Spring Spring是一个开源的Java EE开发框架。Spring框架的核心功能可以应用在任何Jav...
    jemmm阅读 16,561评论 1 133