5
我通常只是@Autowire东西成春天的对象。但是我遇到了需要动态创建一些需要可以自动装配的值的对象。在春天,我可以从autowired bean中自动装入新的bean吗?
我该怎么办?我能做的只是手动将自动装配值传递给新对象的构造函数。我想要做的仅仅是创建每个新对象时自动装入。
@Service
public class Foo {
@Autowired private Bar bar;
/** This creates Blah objects and passes in the autowired value. */
public void manuallyPassValues() {
List<Blah> blahs = new LinkedList<Blah>();
for(int i=0; i<5; ++i) {
Blah blah = new Blah(bar);
blahs.add(blah);
}
// ...
}
/** This creates Blah objects and autowires them. */
public void useAutowire() {
List<Blah> blahs = new LinkedList<Blah>();
for(int i=0; i<5; ++i) {
// How do I implement the createAutowiredObject method?
Blah blah = createAutowiredObject(Blah.class);
blahs.add(blah);
}
// ...
}
}
理想情况下,我不会在这个bean中有任何配置信息。它的自动连接,所以任何物体,它需要在自动装配他们做新豆的自动装配应提供给它