【发布时间】:2017-07-18 06:20:03
【问题描述】:
注释字段/构造函数仅提供类的一个实例,即使 bean 被配置为原型。但我想要循环中特定类的新实例。
下面是我的组件类:
@Component
class Link{
@Autowired
private RandomClass rcobj;
public void getFiveInstancesOfRandomClass(){
//here I want to create five new instances for RandomClass but I get only one by auto-wiring
}
}
配置类
@配置
class ApplicationConfig{
@Bean
public Link link(){ return new Link();}
@Bean
@scope ("prototype")
public RandomClass randomClass(){ return new RandomClass();}
}
我查看了几个主要使用基于 xml 的配置的示例。解决方案之一是调用 ApplicationContext 但我想用查找方法解决这个问题。
【问题讨论】:
标签: java spring configuration annotations