Spring @Resource Handling

允我心安 提交于 2019-12-05 05:04:19

If you are using Spring Stereotype annotations, (@Service, @Component...), then you are probably including in your spring configuration the <context:component-scan /> element to pick them up. It is fine to do this, but it will automatically register a CommonAnnotationBeanPostProcessor with the application context, as stated just above the second note in this link.

The issue with including the CommonAnnotationBeanPostProcessor is that Spring handles the @Resource annotation and will attempt to inject beans from its application context. You can register your own CommonAnnotationBeanPostProcessor bean and tell Spring to allow direct JNDI access to these @Resource's by configuring the bean by setting the alwaysUseJndiLookup property to true.

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
  <property name="alwaysUseJndiLookup" value="true"/>
</bean>

Pay attention to the note in the linked documentation:

NOTE: A default CommonAnnotationBeanPostProcessor will be registered by the "context:annotation-config" and "context:component-scan" XML tags. Remove or turn off the default annotation configuration there if you intend to specify a custom CommonAnnotationBeanPostProcessor bean definition!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!