spring - Problems with @Autowire annotation (null) -
i have problems 2 services autowired in validator class. services works ok because in controller autowired. i've applicationcontext.xml file , myapp-servlet.xml file. base package es.unican.meteo , have problems package es.unican.meteo.validator. package es.unican.meteo.controller , es.unican.meteo.service can autowire services properly.
applicationcontext.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> .... beans ... </beans>
myapp-servlet.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- enabling spring beans auto-discovery --> <context:component-scan base-package="es.unican.meteo" /> <!-- enabling spring mvc configuration through annotations --> <mvc:annotation-driven />
class resetpasswordvalidator:
package es.unican.meteo.validator; import org.springframework.beans.factory.annotation.autowired; import org.springframework.validation.errors; import org.springframework.validation.validator; import es.unican.meteo.model.user; import es.unican.meteo.service.messageservice; import es.unican.meteo.service.userservice; public class resetpasswordvalidation implements validator{ @autowired private userservice userservice; @autowired private messageservice messageservice; public boolean supports(class<?> clazz) { return user.class.equals(clazz); } public void validate(object target, errors errors) { user user = (user)target; if(userservice.getuserbyemail(user.getemail())==null){ errors.rejectvalue("email", messageservice.getmessage("app.error.nonexistentemail")); } } }
i can see controllers, services , autowired elements in spring elements. seems spring not detecting autowired properties in package validator. ideas?
edit: log of resetpasswordvalidation (autowire fields)
12:48:50,697 debug main support.defaultlistablebeanfactory:217 - creating shared instance of singleton bean 'resetpasswordvalidation' 12:48:50,697 debug main support.defaultlistablebeanfactory:430 - creating instance of bean 'resetpasswordvalidation' 12:48:50,701 debug main annotation.injectionmetadata:60 - found injected element on class [es.unican.meteo.validator.resetpasswordvalidation]: autowiredfieldelement private es.unican.meteo.service.userservice es.unican.meteo.validator.resetpasswordvalidation.userservice 12:48:50,702 debug main annotation.injectionmetadata:60 - found injected element on class [es.unican.meteo.validator.resetpasswordvalidation]: autowiredfieldelement private es.unican.meteo.service.messageservice es.unican.meteo.validator.resetpasswordvalidation.messageservice 12:48:50,702 debug main support.defaultlistablebeanfactory:504 - eagerly caching bean 'resetpasswordvalidation' allow resolving potential circular references 12:48:50,707 debug main annotation.injectionmetadata:85 - processing injected method of bean 'resetpasswordvalidation': autowiredfieldelement private es.unican.meteo.service.userservice es.unican.meteo.validator.resetpasswordvalidation.userservice
make sure annotate class spring picks bean. autowiring can occur on beans/classes managed di container.
adding @component
cause class picked spring's component scanning, causing resetpasswordvalidation
become bean. @ point, should eligible have fields autowired.
@component public class resetpasswordvalidation implements validator
Comments
Post a Comment