@Resource(name =“beanName”)vs ApplicationContext#getBean(“beanName”)

时间:2014-02-17 20:33:41

标签: java spring

有人知道使用@ResourcegetBean之间获取bean的过程有何不同?现在我的情况是这样的:

@Autowired
ApplicationContext applicationContext

public void test() {
    Object x = this.applicationContext.getBean("beanName"); //this returns the object I want
}

但这不是:

@Resource(name="beanName")
Object x; //"beanName" can't be found

我想了解这两个过程是如何不同的,这样我就可以开始理解我的错误了。

第二个例子的堆栈跟踪。 mainBean是一个组件扫描的bean,除了我发布的代码之外别无其他。

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainBean': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'beanName' is defined
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:1718)
    at com.ibm.ws.webcontainer.webapp.WebApp.commonInitializationFinish(WebApp.java:385)
    at com.ibm.ws.webcontainer.webapp.WebAppImpl.initialize(WebAppImpl.java:299)
    ... 97 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'beanName' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:442)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:549)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)

1 个答案:

答案 0 :(得分:1)

两个注释都通过bean后处理器工作,需要在与注释bean相同的上下文中应用(请参阅此处的reference):

  • @Autowired通过AutowiredAnnotationBeanPostProcessor
  • 工作
  • @Resource(以及其他注释)通过CommonAnnotationBeanPostProcessor
  • 工作

因此,您看到的行为意味着CommonAnnotationBeanPostProcessor未应用于正在使用@Resource的bean。

CommonAnnotationBeanPostProcessor可以通过以下任一方式激活:

  • 将其明确地添加为特定上下文的bean
  • 通过<context:annotation-config>
  • 通过<context-annotation-scan>

请注意,这需要在与使用@Resource的bean相同的spring上下文中完成。

Spring应用程序可以有多个上下文,例如一个根上下文和多个servlet调度程序上下文,另请参阅此answer

因此,请确保在定义使用@Resource的bean的相同上下文中应用bean post处理器。

相关问题