Maven+Spring : pb avec @Autowired
Bonjour.
Je suis en train de tester Spring Annotations.
Lorsque j'appelle un service, celui-ci est instancié mais pas le dao sous-jacent malgré une annotation @autowired.
Pourtant, lorsque je remplace les annotations par la notation classique dans applicationContext.xml, il n'y a aucun problème.
Voici le contexte :
J'ai fait un projet Maven sous Eclipse.
J'ai importé la dépendance Spring dans le pom.xml:
Code:
1 2 3 4 5 6
|
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency> |
(Déjà je ne comprends pas pourquoi on ne peut pas voir la version 3 dans les repositories.)
Voici mon applicationContext.xml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="fr.vtest.annotations" />
</beans> |
La classe service :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
@Service("userService")
@Scope("singleton")
public class UserServiceImpl implements UserService {
public UserServiceImpl() {
super();
}
private UserDAO userDAO;
@Autowired
public void setUserDAO(@Qualifier("userDAO") UserDAO userDAO) {
this.userDAO = userDAO;
}
} |
qui devrait m'instancier le dao suivant:
Code:
1 2 3 4 5
|
@Repository("userDAO")
@Scope("singleton")
public class UserDAOImpl implements UserDAO {
} |
A l'appel du service j'ai bien le message :
Creating shared instance of singleton bean 'userService',
mais pas celui du dao.
Si j'utilise le fichier xml de manière classique:
Code:
1 2 3 4 5 6
|
<bean id="userService" class="fr.test.service.UserServiceImpl" scope="singleton">
<property name="userDAO">
<ref bean="userDAO" />
</property>
</bean> |
j'ai bien :
Creating shared instance of singleton bean 'userService'
Creating shared instance of singleton bean 'userDAO'
J'ai cru lire qqpart que c'était peut-être dû à l'utilisation de Maven.
J'ai essayé de zapper maven en mettant le jar de spring à la main dans le BuildPath mais ça ne change rien.
Toute piste sera appréciée :)