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!

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...