java - @Resource(名称 ="beanName") 与 ApplicationContext#getBean ("beanName")

标签 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)

最佳答案

两个注释都通过 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 后处理器。已定义。

关于java - @Resource(名称 ="beanName") 与 ApplicationContext#getBean ("beanName"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21838751/

相关文章:

java - JAVA中创建与char数组对应的数组

java - 如果我将一个类的默认构造函数声明为私有(private),为什么它不能被子类化

java - Spring 集成 - 在流之间传递数据

java - Spring @Transactional 注释不适用于 Autowiring ?

java - 考虑在您的配置中定义类型为 'org.o7planning.sbshoppingcart.service.UserDetailsServiceImpl' 的 bean

java - 奇怪的用户崩溃报告: Null Pointer exception for something that is definitely there

java - 如何获得基于整数的位移数

java - Eclipse 项目未添加到构建路径

java - 用于解析 URI 的 Spring 实用程序

java - 带有变音符号的响应文本的内容长度错误