gpt4 book ai didi

java - Spring @Resource 处理

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:23:37 24 4
gpt4 key购买 nike

我在处理注释为 @Resource 的字段时遇到问题在 Spring bean 中。我有什么:

一个字段,带有 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 注入(inject) Spring 管理的 bean。我想让 Spring 处理 @Resource并检索 JNDI 资源。

这是 Spring 2.5.6SEC03,bean 本身被注释为 @Service用于 Autowiring 到其他 @Component实例。在这种情况下,Servlet 容器是 Tomcat 7,但最终将部署到 Weblogic 10 上,因此虽然我希望理想的解决方案能够在两者上运行,但 Weblogic 是必备的。

我是否滥用了 Spring 2.5 中的这个功能?一般来说?有什么我想念的吗?我对 JNDI 有什么误解?感谢所有帮助。谢谢。

最佳答案

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

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

<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!

关于java - Spring @Resource 处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9931491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com