我刚刚从这篇文章(http://www.infoq.com/articles/spring-2.5-part-1)了解@Resource
注释,并希望利用它在Tomcat 6.0.26和3.0.3春季数据源不注入@Resource注释字段alwaysUseJndiLookup设置
但它确实不工作 - 字段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
我不知道为什么它不起作用。
注:默认CommonAnnotationBeanPostProcessor会将由注册“背景:注解配置”和“上下文:组件扫描” XML标签我documentation这个发现。如果您打算指定自定义的CommonAnnotationBeanPostProcessor bean定义,请在那里删除或关闭默认的注释配置!
我认为这可能是关于我的问题,但在这种情况下,我不知道如何“删除或关闭默认注释配置”。
请帮忙。 在此先感谢!
我不确定这会解决你的问题,你可以尝试将'ds'改为someting,someting more 2 char? – 2010-09-27 15:29:47
@Jaydeep:谢谢。不,没有任何改变,因为我将变量从ds更名为dataSource。 – 2010-09-27 16:46:47