Spring 自动连线bean分别和作为列表实例化

Spring 自动连线bean分别和作为列表实例化,spring,Spring,我在@Configuration中对bean进行了如下实例化 @Bean public Queue queue1() { return new Queue("queue1"); } @Bean public Queue queue2() { return new Queue("queue2"); } @Bean public List<Queue> queues(List<String> names) { List<Queue> ret = n

我在@Configuration中对bean进行了如下实例化

@Bean
public Queue queue1() {
  return new Queue("queue1");
}

@Bean
public Queue queue2() {
  return new Queue("queue2");
}

@Bean
public List<Queue> queues(List<String> names) {
  List<Queue> ret = new LinkedList<>();
  for (String name : names) {
    ret.add(new Queue(name));
  }
  return ret;
}
@Bean
公共队列队列1(){
返回新队列(“队列1”);
}
@豆子
公共队列队列2(){
返回新队列(“队列2”);
}
@豆子
公共列表队列(列表名称){
List ret=new LinkedList();
for(字符串名称:名称){
ret.add(新队列(名称));
}
返回ret;
}
当我@Autowire他们如下

@Autowired
private Collection<Queue> queues;
@Autowired
私人收集队列;

@Autowired
私有列表队列;
我希望得到所有(分别实例化的和作为实例实例化的) 一个列表)但我只得到单独实例化的

你能给个建议吗

注意:


如果我使用@Qualifier,我可以让列表中的那些自动连线,但只有那些。我正在寻找一种方法来自动连接它们

最后,我通过
后期施工
获得了我需要的东西:

@Autowired
private QueueNames queueNames;

@Autowired
private ConfigurableBeanFactory beanFactory;

@PostConstruct
public void queues() {
  for (String name : queueNames.get()) {
    beanFactory.registerSingleton(name, new Queue(name));
  }
}

最后,我通过
PostConstruct
获得了我所需要的:

@Autowired
private QueueNames queueNames;

@Autowired
private ConfigurableBeanFactory beanFactory;

@PostConstruct
public void queues() {
  for (String name : queueNames.get()) {
    beanFactory.registerSingleton(name, new Queue(name));
  }
}

你想实现什么?这对我来说似乎是个坏主意。@galovics我事先知道几个队列的名字,但我得到的其他名字都是列表。然后我想遍历所有这些,您应该将它们作为bean单独添加。您应该使用
BeanRegistryPostProcessor
BeanFactoryPostProcessor
来执行此操作。将它们作为列表公开不会使spring管理bean(其中的单个
队列
s)成为受管理bean。因此,您要从代码中手动调用此方法public list queues(list names){..}。如果是这样,那么声明为@Bean就没有意义了。在这种情况下,Spring无法自动连接它cases@pvpkiran不,我不是手动打电话。由于
@Bean
注释,spring框架调用了该代码。您想实现什么?这对我来说似乎是个坏主意。@galovics我事先知道几个队列的名字,但我得到的其他名字都是列表。然后我想遍历所有这些,您应该将它们作为bean单独添加。您应该使用
BeanRegistryPostProcessor
BeanFactoryPostProcessor
来执行此操作。将它们作为列表公开不会使spring管理bean(其中的单个
队列
s)成为受管理bean。因此,您要从代码中手动调用此方法public list queues(list names){..}。如果是这样,那么声明为@Bean就没有意义了。在这种情况下,Spring无法自动连接它cases@pvpkiran不,我不是手动打电话。由于
@Bean
注释,spring框架调用了该代码。