我有兴趣从 Spring 获取 bean 列表ApplicationContext。特别是这些是Ordered豆类
@Component
@Order(value=2)
Run Code Online (Sandbox Code Playgroud)
而且我在一些未启用 Spring 的遗留代码中,所以我制造了一种方法来获取ApplicationContext. 对于春豆,我知道我可以这样做:
@Bean
public SomeType someType(List<OtherType> otherTypes) {
SomeType someType = new SomeType(otherTypes);
return someType;
}
Run Code Online (Sandbox Code Playgroud)
但ApplicationContextonly 提供了一种getBeansOfType返回无序映射的方法。我试过,getBeanNames(type)但这也会返回无序的东西。
我唯一能想到的是创建一个只包含 List 的虚拟类,为该虚拟类创建一个 bean 并检索有序列表:
public class DumbOtherTypeCollection {
private final List<OtherType) otherTypes;
public DumbOtherTypeCollection(List<OtherType) otherTypes) {
this.otherTypes = otherTypes;
}
List<OtherType> getOrderedOtherTypes() { return otherTypes; }
}
@Bean
DumbOtherTypeCollection wasteOfTimeReally(List<OtherType otherTypes) {
return new DumbOtherTypeCollection(otherTypes);
}
....
applicationContext.getBean(DumbOtherTypeCollection.class).getOrderedOtherTypes();
Run Code Online (Sandbox Code Playgroud)
只是希望我能做得更好。