DataSource 未使用alwaysUseJndiLookup 设置注入@Resource 注释字段

发布于 2024-09-24 18:54:56 字数 3642 浏览 2 评论 0 原文

我刚刚阅读了这篇文章中的 @Resource 注释 (http://www.infoq.com/articles/spring-2.5-part-1)并希望在 Tomcat 6.0.26 和 Spring 3.0.3 上使用它

,但它不起作用 -- field Users 类中的 >ds 未初始化,当我尝试进行查询时出现 NullPointerException

文件src/org/example/db/Users.java

package org.example.db;
import javax.sql.DataSource;
import javax.annotation.Resource;

@Repository
public class Users {

 @Resource private DataSource ds;
 ...
}

文件WEB-INF/spring-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/jee
 http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan base-package="org.example.web.controller,org.example.db" />

 <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
  <property name="alwaysUseJndiLookup" value="true" />
 </bean>

 <jee:jndi-lookup id="ds" jndi-name="java:comp/env/jdbc/mydb" />

</beans>

文件WEB-INF/web.xml >

 <resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/mydb</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>

在日志文件中:

DEBUG 2010-09-27 21:56:00,085: Creating shared instance of singleton bean 'ds'
DEBUG 2010-09-27 21:56:00,085: Creating instance of bean 'ds'
DEBUG 2010-09-27 21:56:00,086: Eagerly caching bean 'ds' to allow for resolving potential circular references
DEBUG 2010-09-27 21:56:00,106: Invoking afterPropertiesSet() on bean with name 'ds'
DEBUG 2010-09-27 21:56:00,116: Finished creating instance of bean 'ds'
DEBUG 2010-09-27 21:56:00,149: Found injected element on class [org.example.db.Users]: ResourceElement for private javax.sql.DataSource org.example.db.Users.ds
DEBUG 2010-09-27 21:56:00,152: Found injected element on class [org.example.db.Users]: ResourceElement for private javax.sql.DataSource org.example.db.Users.ds
DEBUG 2010-09-27 21:56:00,161: Processing injected method of bean 'users': ResourceElement for private javax.sql.DataSource org.example.db.Users.ds
DEBUG 2010-09-27 21:56:00,163: Returning cached instance of singleton bean 'ds'
DEBUG 2010-09-27 21:56:00,442: Returning cached instance of singleton bean 'ds'
DEBUG 2010-09-27 21:56:00,593: Rejected bean name 'ds': no URL paths identified
DEBUG 2010-09-27 21:56:00,738: Rejected bean name 'ds': no URL paths identified

我不知道为什么它不起作用。我在 文档< /a> 这个:

注意:默认的 CommonAnnotationBeanPostProcessor 将通过“context:annotation-config”和“context:component-scan”XML 标记注册。如果您打算指定自定义 CommonAnnotationBeanPostProcessor bean 定义,请删除或关闭那里的默认注释配置!

我认为这可能是关于我的问题,但在这种情况下我不知道如何“删除或关闭默认注释配置”。

请帮忙。 提前致谢!

I've just read about @Resource annotation from this article (http://www.infoq.com/articles/spring-2.5-part-1) and wish to use it on Tomcat 6.0.26 and Spring 3.0.3

But it does not work -- field ds in Users class does not initialized and I've got NullPointerException when I try to made a query.

File src/org/example/db/Users.java

package org.example.db;
import javax.sql.DataSource;
import javax.annotation.Resource;

@Repository
public class Users {

 @Resource private DataSource ds;
 ...
}

File WEB-INF/spring-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/jee
 http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan base-package="org.example.web.controller,org.example.db" />

 <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
  <property name="alwaysUseJndiLookup" value="true" />
 </bean>

 <jee:jndi-lookup id="ds" jndi-name="java:comp/env/jdbc/mydb" />

</beans>

File WEB-INF/web.xml

 <resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/mydb</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>

In log file:

DEBUG 2010-09-27 21:56:00,085: Creating shared instance of singleton bean 'ds'
DEBUG 2010-09-27 21:56:00,085: Creating instance of bean 'ds'
DEBUG 2010-09-27 21:56:00,086: Eagerly caching bean 'ds' to allow for resolving potential circular references
DEBUG 2010-09-27 21:56:00,106: Invoking afterPropertiesSet() on bean with name 'ds'
DEBUG 2010-09-27 21:56:00,116: Finished creating instance of bean 'ds'
DEBUG 2010-09-27 21:56:00,149: Found injected element on class [org.example.db.Users]: ResourceElement for private javax.sql.DataSource org.example.db.Users.ds
DEBUG 2010-09-27 21:56:00,152: Found injected element on class [org.example.db.Users]: ResourceElement for private javax.sql.DataSource org.example.db.Users.ds
DEBUG 2010-09-27 21:56:00,161: Processing injected method of bean 'users': ResourceElement for private javax.sql.DataSource org.example.db.Users.ds
DEBUG 2010-09-27 21:56:00,163: Returning cached instance of singleton bean 'ds'
DEBUG 2010-09-27 21:56:00,442: Returning cached instance of singleton bean 'ds'
DEBUG 2010-09-27 21:56:00,593: Rejected bean name 'ds': no URL paths identified
DEBUG 2010-09-27 21:56:00,738: Rejected bean name 'ds': no URL paths identified

I don't know why it does not work. I found in documentation this:

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!

I think it may be about my problem, but in this case I don't know how to "Remove or turn off the default annotation configuration".

Please help.
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2024-10-01 18:54:56

目前这个问题已经解决了。

所有这些代码都有效,但我认为问题出在一些相关的类中。例如,我有以下自动装配层次结构:dataSource 注入到 Users 类,Users 类注入到 Validator,从 Controller 调用 Validator。在这个链中存在一些错误:

  • 在 @Autowiring 传播之后使用新的 Validator 实例创建用户
  • ,用户没有注入到 Validator,因为此类位于另一个包中,并且组件扫描稍后找不到它
  • 用户没有注入到 Validator,因为这个类没有 @Component 注释
  • 控制器使用默认构造函数创建验证器的新实例

在我解决所有这些问题之后,一切正常。

For now this issue is resolved.

All this code works, but I think the problem was in some of the related classes. For example, I have following autowiring hierarchy: dataSource injects to Users class, Users class injects to Validator, Validator is called from Controller. In this chain were a few bugs:

  • Users are created using a new Validator instance
  • after @Autowiring propagation, User did not inject to Validator because this class was in another package and component-scan not find it
  • later User did not inject to Validator because this class does not have @Component annotation
  • Controller creates new instance of Validator using the default constructor

After I resolve all these issues all works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文