微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Spring @Resource Handling

我在 Spring bean中注释为@Resource的字段时遇到了问题.是)我有的:

一个字段,带有setter方法,带注释的@Resource

@Resource
private URL someUrl;

public void setSomeUrl(URL someUrl) {
    this.someUrl = someUrl;
}

< env-entry>我的部署描述符中的标记(web.xml)

<env-entry>
    <env-entry-name>someUrl</env-entry-name>
    <env-entry-type>java.net.URL</env-entry-type>
    <env-entry-value>http://somedomain.net/some/path</env-entry-value>
</env-entry>

应用程序无法以BeanCreationException开始,我不希望这是因为我不一定希望spring注入Spring管理的bean.我希望Spring处理@Resource并检索JNDI资源.

这是Spring 2.5.6SEC03,bean本身是带注释的@Service,用于自动连接到其他@Component实例.在这种情况下,Servlet容器是Tomcat 7,但最终将部署到Weblogic 10上,所以虽然我希望理想的解决方案同时适用于两者,但Weblogic是必备的.

我在Spring 2.5中滥用此功能吗?一般来说?我有点遗失吗?我误解了JNDI的一些事情?所有帮助表示赞赏.谢谢.

解决方法

如果您正在使用Spring Stereotype注释(@ Service,@ Component …),那么您可能在弹簧配置中包含< context:component-scan />拾取它们的元素.这样做很好,但它会自动在应用程序上下文 as stated just above the second note in this link注册 CommonAnnotationBeanPostProcessor.

包含CommonAnnotationBeanPostProcessor的问题是Spring处理@Resource注释并将尝试从其应用程序上下文中注入bean.您可以注册自己的CommonAnnotationBeanPostProcessor bean,并通过将alwaysUseJndiLookup属性设置为true来配置bean,从而告诉Spring允许直接JNDI访问这些@ Resource.

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

请注意链接文档中的注释:

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!

原文地址:https://www.jb51.cc/java/129913.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐