Spring @Resource Handling

Mik*_*key 7 java spring tomcat weblogic jndi

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

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

@Resource
private URL someUrl;

public void setSomeUrl(URL someUrl) {
    this.someUrl = someUrl;
}
Run Code Online (Sandbox Code Playgroud)

<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>
Run Code Online (Sandbox Code Playgroud)

应用程序无法以a开头BeanCreationException,我没想到,因为我不一定要Spring注入一个Spring管理的bean.我希望Spring处理@Resource和检索JNDI资源.

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

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

nic*_*ild 7

如果您正在使用Spring Stereotype注释(@Service,@Component...),那么您可能会在弹簧配置中包含<context:component-scan />拾取它们的元素.这样做很好,但它会自动注册一个CommonAnnotationBeanPostProcessor应用程序上下文,如本链接第二个注释上面所述.

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

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
  <property name="alwaysUseJndiLookup" value="true"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

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

注意:默认的CommonAnnotationBeanPostProcessor将由"context:annotation-config"和"context:component-scan"XML标记注册.如果要指定自定义CommonAnnotationBeanPostProcessor bean定义,请删除或关闭默认注释配置!