Bonjour à tous,

J'ai bien consulté les sujets concernant mon problème et je n'ai pas réussi à trouver la réponse, c'est pourquoi je me permet d'énoncer mon soucis.

En effet l'annotation @Autowired dans mon cas ne fonctionne que quand elle est déclarée dans ma classe de test unitaire, je m'explique.

Voici mes classes pseudo metier

Une classe de test qui devrait provoquer une injection :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package fr.npa.annuaire.domain;
 
import org.springframework.beans.factory.annotation.Autowired;
 
public class MaClasseAutowired {
 
	@Autowired
	private Toto toto;
 
	public Toto getToto() {
 
		return toto;
	}
 
	public void setToto(Toto toto) {
 
		this.toto = toto;
	}
 
	public MaClasseAutowired(){
 
		System.out.println("####  Constructeur MaClasseAutowired");
	}
}
La classe Toto qui est marquée comme Component :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package fr.npa.annuaire.domain;
 
import org.springframework.stereotype.Component;
 
 
@Component
public class Toto {
 
	//@Autowired
	//CrudRepository<Personne, Integer> repoTOTO;
 
	public Toto(){
 
		System.out.println("#### CONSTRUCTEUR TOTO");
	}
}
Voici ma classe de test unitaire :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package fr.npa.annuaire.siteWeb.actions;
 
import static org.junit.Assert.assertNotNull;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import fr.npa.annuaire.domain.MaClasseAutowired;
import fr.npa.annuaire.domain.Toto;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/applicationContext.xml" })
public class AutowiredTest {
 
	@Autowired
	private Toto totoTest;
 
	@Test
	public void testAutowire(){
 
		assertNotNull("totoTest null", totoTest);
 
                //la ca passe, l'autowired a bien fonctionne
		MaClasseAutowired tmp = new MaClasseAutowired();
 
                //et la ca casse
		assertNotNull("toto est null", tmp.getToto());
	}
}
Lors de l’exécution l'attribut totoTest a bien été instancié par injection mais le deuxième assert ne passe pas car l'autowired ne s'est pas fait dans MaClasseAutowired.

Voici mes fichiers de configurations

web.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="struts_blank" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Struts Blank Convention</display-name>
 
 
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
 
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>
 
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>struts.devMode</param-name>
			<param-value>true</param-value>
		</init-param>
<!-- 		<init-param>
			<param-name>struts.objectFactory</param-name>
			<param-value>org.apache.struts2.spring.StrutsSpringObjectFactory</param-value>
		</init-param> -->
	</filter>
 
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
 
	<!-- <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> -->
</web-app>
Le fichier applicationContext.xml contient entre autre :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
<bean 
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
	<context:component-scan base-package="fr.npa.annuaire"/>

Je cherche depuis ce matin a résoudre ce drôle de problème, je pense que l'erreur va vous sauter aux yeux :p

En vous remerciant!