1
注释字段/构造函数仅提供该类的一个实例,即使该bean被配置为原型。但是我想要一个循环中的特定类的新实例。在基于java的配置中使用@ Lookup-method
下面是我的组件类:
@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
}
}
Config.class
@Configuration
class ApplicationConfig{
@Bean
public Link link(){ return new Link();}
@Bean
@scope ("prototype")
public RandomClass randomClass(){ return new RandomClass();}
}
我看着它主要使用基于XML配置几个例子。其中一个解决方案是调用ApplicationContext,但我想用lookup-method来解决这个问题。
谢谢。我还发现了一个更好的解决方案https://stackoverflow.com/questions/14880847/howto-generate-prototype-objects-with-in-a-singleton-bean-using-spring-java-conf – Struse