给定spring java-config with method返回bean列表:
@Configuration
public class Config {
@Bean(name = "resultConsumers")
public List<Consumer> getConsumers() {
return Arrays.asList(...);
}
}
如何将其注入另一个bean?
class Bean {
@Inject
what?
}
P.S。它不是具有不同消费者实现的列表,它们都是同一类的实例。
答案 0 :(得分:2)
当您使用Collection
注释@Autowired
类型时,Spring不会查找该类型的相应bean。相反,它会查找Collection
要存储的组件类型。
相反,请将@Resource
与bean名称一起使用。
@Resource(name ="resultConsumers")
private List<Consumer> consumers;
答案 1 :(得分:-1)
You should not create the @Bean as a list. Simply add all of your Consumer objects as individual beans. Autowire a field as a List of Consumers, and Spring will find all of your Consumer beans and create the List for you...
@Configuration
public class Config {
@Bean
public Consumer consumer1() {
return new Consumer();
}
@Bean
public Consumer consumer2() {
return new Consumer();
}
}
...
class Bean {
// Contains consumer1 and consumer2
@Inject
private List<Consumer> consumers;
}
答案 2 :(得分:-1)
<?php
$n = ( isset($_GET['n']) ? $_GET['n'] : "");
echo $n;
?>
注射就像这样
@Configuration
public class Config {
@Bean
public List<Consumer> getConsumers() {
return Arrays.asList(...);
}
}
这就是全部。