加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Spring @Resource Handling

发布时间:2020-12-15 00:51:59 所属栏目:Java 来源:网络整理
导读:我在 Spring bean中注释为@Resource的字段时遇到了问题.是)我有的: 一个字段,带有setter方法,带注释的@Resource @Resourceprivate URL someUrl;public void setSomeUrl(URL someUrl) { this.someUrl = someUrl;} env-entry我的部署描述符中的标记(web.xml)
我在 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!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读