从初始化的 bean 对象中获取限定符名称 [英] Getting qualifier names from initialized bean objects
问题描述
我有两个相同类型的豆子;
I have two beans of the same type;
@Bean
public RestTemplate jsonTemplate() {
return new RestTemplate();
}
@Bean
public RestTemplate xmlTemplate() {
return new RestTemplate();
}
我将两个 bean 自动装配到一个列表中,如下所示;
And I autowire both beans into a list as follows;
@Autowired
private List<RestTemplate> templates;
列表 templates
将包含 size=2
的两个 bean.
The list templates
will have both beans inside with size=2
.
从这个列表中,我怎样才能得到他们的名字 (["jsonTemplate", "xmlTemplate"
])?
From this list, how can I get their names (["jsonTemplate", "xmlTemplate"
])?
推荐答案
真的很简单...
只是做;
@Autowired
private Map<String, RestTemplate> templates;
将让 Spring 将名称作为键插入,并将 bean 本身作为值插入地图.
will let Spring to insert the names as keys and the beans themselves as the values in the map.
似乎 Spring 只是在注入后停止跟踪命名.所以我不知道是否还有其他方法(或者,如果可能的话,更简单的方法)比这更简单?
It seems Spring just stops keeping track of the naming after the injection. So I don't know if there is any other way (or, if even possible, simpler way) than this?
这篇关于从初始化的 bean 对象中获取限定符名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!