Bonjour à tous,
J'ai une classe nommée RechercheInfosTableImpl, et annotée @Component
Cette classe présente un attribut de type Map<String, String>
Je voudrais uniquement par annotation, instancier cet attribut et l'injecter lors de l'instanciation via Spring de la classe RechercheInfosTableImpl.
Voici mon code qui ne marche pas du tout (j'ai essayé plein d'options qui ne marchent pas et plus cela va, plus ma lucidité diminue) :
Voici les erreurs obtenues à l'exécution :
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 package fr.msa.agora.bp0gos.local.lanceur; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import fr.cnamts.rfos.nomenclature.CodeValeur; import fr.cnamts.rfos.nomenclature.Table; import fr.cnamts.rfos.nomenclature.Valeurs; /** * Service de recherche d'informations sur les tables. * * */ @Component public class RechercheInfosTableImpl implements RechercheInfosTable { /** * Map [clé = "Nom d'une Table" , valeur = "Code Qualifiant associé à cette table"]. */ @Resource private final Map<String, String> tableauAssociatifNomTableETCodeQualifiantTable; /** * @param pTableauAssociatifNomTableETCodeQualifiantTable */ @Autowired public RechercheInfosTableImpl(final Map<String, String> pMap) { tableauAssociatifNomTableETCodeQualifiantTable = pMap; } /** * Alimente la Map [clé = "Nom d'une table" , valeur = "Code Qualifiant de cette table"] * * @param pNomTable table CNAM lue. */ @Override public void extraireInfosTable(final Table pTable) { synchronized (tableauAssociatifNomTableETCodeQualifiantTable) { // si la table lue n'est pas stockée dans le tableau associatif if (!tableauAssociatifNomTableETCodeQualifiantTable.containsKey(pTable.getNom())) { boolean valeurTrouvee = false; final Iterator<Valeurs> iterateurValeurs = pTable.getValeurs().iterator(); Valeurs valeurLue; // une valeur = une liste de codes // parcourir les valeurs de cette map // jusqu'à ce qu'on arrive au bout de toutes ces valeurs (ces listes de codes) // ou bien qu'on tombe, pour une de ces valeurs, sur un code principal while (iterateurValeurs.hasNext() && !valeurTrouvee) { valeurLue = iterateurValeurs.next(); // si la liste de codes de la valeur lu n'est pas vide if ((valeurLue != null) && (valeurLue.getCode() != null) && (!valeurLue.getCode().isEmpty())) { final Iterator<CodeValeur> iterateurCodesValeur = valeurLue.getCode().iterator(); while (iterateurCodesValeur.hasNext() && !valeurTrouvee) { final CodeValeur codeValeurlue = iterateurCodesValeur.next(); // si on trouve pour la valeur lue, un code principal if ((codeValeurlue != null) && ("P".equalsIgnoreCase(codeValeurlue.getR())) && (!codeValeurlue.getQ().isEmpty())) { // associer, dans le tableur associatif attribut // le nom de la table // et le code qualifiant de cette table tableauAssociatifNomTableETCodeQualifiantTable.put(pTable.getNom(), codeValeurlue.getQ()); valeurTrouvee = true; } } } } } } } /* * (non-Javadoc) * * @see fr.msa.agora.bp0gos.local.lanceur.RechercheInfosTable#afficherInformations() */ @Override public List<String> afficherInformations() { final List<String> listeAAfficher = new ArrayList<String>(); for (final Entry<String, String> element : tableauAssociatifNomTableETCodeQualifiantTable.entrySet()) { listeAAfficher.add("Table => Nom : " + element.getKey() + " Code Qualifiant : " + element.getValue()); } return listeAAfficher; } }
En utilisant uniquement des annotations (sans toucher à des fichiers XML), comment faire pour injecter ma map dans mon service ?
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
37
38
39
40
41
42 Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'lanceurApplicationExtractionDonneesTable' defined in file [D:\platformsg2_R_64\workspace\gos-injecteur-flux-rfos\target\classes\fr\msa\agora\bp0gos\local\lanceur\LanceurApplicationExtractionDonneesTable.class]: Unsatisfied dependency expressed through constructor argument with index 2 of type [fr.msa.agora.bp0gos.local.lanceur.RechercheInfosTable]: : Error creating bean with name 'rechercheInfosTableImpl' defined in file [D:\platformsg2_R_64\workspace\gos-injecteur-flux-rfos\target\classes\fr\msa\agora\bp0gos\local\lanceur\RechercheInfosTableImpl.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.Map]: : No matching bean of type [java.lang.String] found for dependency [map with value type java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency [map with value type java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rechercheInfosTableImpl' defined in file [D:\platformsg2_R_64\workspace\gos-injecteur-flux-rfos\target\classes\fr\msa\agora\bp0gos\local\lanceur\RechercheInfosTableImpl.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.Map]: : No matching bean of type [java.lang.String] found for dependency [map with value type java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency [map with value type java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:718) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:194) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:993) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:897) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:574) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) at fr.msa.agora.bp0gos.local.lanceur.GestionApplicationStatic.chargementSpring(GestionApplicationStatic.java:36) at fr.msa.agora.bp0gos.local.lanceur.LanceurApplicationExtractionDonneesTable.chargerSpring(LanceurApplicationExtractionDonneesTable.java:128) at fr.msa.agora.bp0gos.local.lanceur.LanceurApplicationExtractionDonneesTable.main(LanceurApplicationExtractionDonneesTable.java:60) Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rechercheInfosTableImpl' defined in file [D:\platformsg2_R_64\workspace\gos-injecteur-flux-rfos\target\classes\fr\msa\agora\bp0gos\local\lanceur\RechercheInfosTableImpl.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.Map]: : No matching bean of type [java.lang.String] found for dependency [map with value type java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency [map with value type java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:718) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:194) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:993) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:897) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:838) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:780) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:697) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:784) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:711) ... 15 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency [map with value type java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:914) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:770) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:697) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:784) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:711) ... 29 more
En effet, On me demande de n'utiliser que des annotations.
Si j'enlève le constructeur annoté @Autowired, cela ne passe même plus à la compilation.
D'avance Merci ,
Thomas
Partager