Spring @Resource处理 [英] Spring @Resource Handling
问题描述
我在Spring Bean中遇到一个标注为@Resource
的字段时遇到了麻烦.我所拥有的:
I'm having trouble with a field annotated as @Resource
in a Spring bean. What I have:
带有设置方法的字段,其注释为@Resource
A field, with setter method, annotated @Resource
@Resource
private URL someUrl;
public void setSomeUrl(URL someUrl) {
this.someUrl = someUrl;
}
我的部署描述符(web.xml)中的<env-entry>
标记
An <env-entry>
tag in my deployment descriptor (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资源.
The application fails to start with a BeanCreationException
, which I dont' expect because I don't necessarily want spring to inject a Spring-managed bean. I want Spring to process @Resource
and retrieve the JNDI resource.
这是Spring 2.5.6SEC03,bean本身被注释为@Service
,用于自动装配到其他@Component
实例中.在这种情况下,Servlet容器是Tomcat 7,但最终将部署到Weblogic 10上,因此,尽管我理想地希望在两者上都可以使用一种解决方案,但Weblogic是必不可少的.
This is Spring 2.5.6SEC03 and the bean itself is annotated @Service
for autowiring into other @Component
instances. Servlet container in this case is Tomcat 7 but will ultimately be deployed onto Weblogic 10, so while I'd like ideally for a solution to work on both, Weblogic is the must-have.
我在Spring 2.5中滥用此功能吗?一般来说?我有什么想念的吗?我对JNDI有误解吗?感谢所有帮助.谢谢.
Am I misusing this feature in Spring 2.5? In general? Is there some bit I'm missing? Something I misunderstand about JNDI? All help is appreciated. Thanks.
推荐答案
如果您正在使用Spring Stereotype批注(@Service
,@Component
...),那么您可能在弹簧配置中包括了
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.
包含CommonAnnotationBeanPostProcessor
的问题是Spring处理@Resource
批注,并将尝试从其应用程序上下文中注入bean.您可以注册自己的CommonAnnotationBeanPostProcessor
bean,并通过将alwaysUseJndiLookup
属性设置为true
来配置bean,告诉Spring允许直接JNDI访问这些@Resource
.
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:
注意:默认的CommonAnnotationBeanPostProcessor将通过"context:annotation-config"和"context:component-scan" XML标签进行注册.如果要指定自定义CommonAnnotationBeanPostProcessor 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!
这篇关于Spring @Resource处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!