【问题标题】:@Resource(name="beanName") vs ApplicationContext#getBean("beanName")@Resource(name="beanName") 与 ApplicationContext#getBean("beanName")
【发布时间】:2014-03-17 07:40:38
【问题描述】:

有谁知道使用@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)

【问题讨论】:

  • 请为您的第二个 sn-p 发布完整的异常堆栈跟踪。
  • 这对您有帮助吗? blogs.sourceallies.com/2011/08/…。它解释了@Resource@Inject@Autowired 如何解决依赖关系,但是在答案中详细说明有点长,只要链接可用,我不觉得要总结。另外,如果没有更多信息(堆栈跟踪、配置等),我现在无法提供更多帮助!
  • 这些注入目标在哪里?哪些课?向我们展示更大的图景。
  • 按预期工作。在您向我们展示的内容之间,您一定在做其他事情。
  • @SotiriosDelimanolis 我只是想了解一些关于这如何可能的想法(缺少更改 Spring 源代码)。假设您故意尝试破坏 Spring 并导致这种情况,您对如何做到这一点有任何想法吗?作为答案,这本身对我来说是令人满意的。

标签: java spring


【解决方案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 后处理器。

【讨论】:

    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 2010-09-19
    相关资源
    最近更新 更多