I want to inject a map in yml file like that ;
abc-identify:
test:
51L: anhuiAbcIdentifyRule
my config class like that
@ConfigurationProperties(prefix = "abc-identify")
@Component
@Data
public class AbcIdentifyConfig {
private Map<Long, IdentifyRule> test;
anhuiAbcIdentifyRule is a existing bean in container
@Component
public class AnhuiAbcIdentifyRule implements IdentifyRule
I tried above setting which not work,how can I resolve this?
Spring does not support this type of string to bean conversion yet. Your code need to be changed to
private Map<Long, String> test;
If you want to get bean by rule from properties, there is a workaround.
@Data
@Component
@ConfigurationProperties(prefix = "abc-identify")
public class AbcProperties {
private Map<Long, String> test;
@Autowired
private Map<String, IdentifyRule> identifyRuleMap;
public IdentifyRule getRule(Long rule){
String beanName = test.get(rule);
if(beanName != null){
return identifyRuleMap.get(beanName);
}
return null;
}
}
Here Map<String, IdentifyRule> identifyRuleMap
will contain all beans of IdentityRule
with keys as beanName
and values as bean
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.