2017-04-26 53 views
1

我有一个@Configuration自动装配豆无论是单独和列表实例

@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; 
} 

实例如下豆当我@Autowire他们像下面

@Autowired 
private Collection<Queue> queues; 

@Autowired 
private List<Queue> queues; 

我期望得到所有的(都是单独实例化的,并且作为 的列表)他们但我只得到那些单独实例化。

你能提醒一下吗?

注:

如果我使用@Qualifier我可以列表那些自动装配,但只有这些。我正在寻找一种自动装配它们的方法。

+0

那你想达到什么目的?这对我来说似乎是一个坏主意。 – galovics

+0

@galovics我知道队列apriori夫妇的名字,但我得到的名单的其余名单。然后我想遍历所有这些。 –

+0

你应该将它们单独添加为bean。您应该使用'BeanRegistryPostProcessor'或'BeanFactoryPostProcessor'来执行此操作。将它们公开为一个列表将不会使Spring管理的bean(单独的'队列'在那里)。 –

回答

0

在我设法得到什么,我需要使用PostConstruct末:

@Autowired 
private QueueNames queueNames; 

@Autowired 
private ConfigurableBeanFactory beanFactory; 

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