spring mvc - DataSource not injected to @Resource annotated field with alwaysUseJndiLookup setting -
i've read @resource
annotation article (http://www.infoq.com/articles/spring-2.5-part-1) , wish use on tomcat 6.0.26 , spring 3.0.3
but not work -- field ds
in users
class not initialized , i've got nullpointerexception
when try made 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' allow resolving potential circular references debug 2010-09-27 21:56:00,106: invoking afterpropertiesset() on bean 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 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 private javax.sql.datasource org.example.db.users.ds debug 2010-09-27 21:56:00,161: processing injected method of bean 'users': resourceelement 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 not work. found in documentation this:
note: default commonannotationbeanpostprocessor registered "context:annotation-config" , "context:component-scan" xml tags. remove or turn off default annotation configuration there if intend specify custom commonannotationbeanpostprocessor bean definition!
i think may problem, in case don't know how "remove or turn off default annotation configuration".
please help. in advance!
for issue resolved.
all code works, think problem in of related classes. example, have following autowiring hierarchy: datasource injects users class, users class injects validator, validator called controller. in chain few bugs:
- users created using new validator instance
- after @autowiring propagation, user did not inject validator because class in package , component-scan not find it
- later user did not inject validator because class not have @component annotation
- controller creates new instance of validator using default constructor
after resolve these issues works fine.
Comments
Post a Comment