我刚刚阅读了本文(http://www.infoq.com/articles/spring-2.5-part-1)中的@Resource
注释,并希望在Tomcat 6.0.26和Spring 3.0.3上使用它
但它不起作用 - ds
类中的字段Users
未初始化,当我尝试进行查询时,我有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
我不知道为什么它不起作用。我在documentation找到了这个:
注意:默认的CommonAnnotationBeanPostProcessor将由“context:annotation-config”和“context:component-scan”XML标记注册。如果要指定自定义CommonAnnotationBeanPostProcessor bean定义,请删除或关闭默认注释配置!
我认为这可能与我的问题有关,但在这种情况下我不知道如何“删除或关闭默认的注释配置”。
请帮忙。 提前谢谢!
答案 0 :(得分:0)
现在这个问题已经解决了。
所有这些代码都有效,但我认为问题出在一些相关的类中。例如,我有以下自动装配层次结构:dataSource注入到Users类,Users类注入Validator,Validator从Controller调用。在这个链中有一些错误:
解决所有这些问题后,一切正常。