[B]我有一个名为RechercheInfosTableImpl的类,并注释了@Component 该类有一个Map类型的属性 我只想通过注释,实例化这个属性,并在使用类RechercheInfosTableImpl的实例化时注入它。[/ B]
这是我的班级,它不起作用(我尝试了很多选项,他们不工作,我放松了我的清醒):
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 {
[B]/**
* 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;
}[/B]
/**
* 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;
}
}
在执行程序时遇到错误:
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
仅使用注释(不修改XML文件),如何在我的服务中注入我的Map属性? 事实上,有人要求我只使用注释。
如果我使用@Autowired注释删除构造函数,即使编译也不起作用。
提前致谢, 托马斯
答案 0 :(得分:2)
我不知道你要从代码中做什么。但是在春天注入Map
需要Map<String, Class<?>>
签名。班级必须是interface
。然后,Spring会自动将每个impl类及其指定的bean名称连接到该映射中。
目前您正在尝试注入String
,这是不可能的,因为字符串不是弹簧管理对象。
或者,如果我误解了您并且您想要真正注入Map<String, String>
:创建一个包含您希望注入的地图的包装类,例如:
@Component
class MapWrapper {
private Map<String, String> map;
}
答案 1 :(得分:1)
尝试另外使用@Qualifier("[name of map]")
注释{/ 1}}。
Spring有时会遇到具体类型的问题。在我的情况下,我需要创建一个@Resource
类型的setter,注入工作正常。