@Configuration
public class MyConfig {
@Bean(name = "myObj")
public MyObj getMyObj() {
return new MyObj();
}
}
我有@Configuration Spring批注的此MyConfig对象。 我的问题是,我怎样才能以编程方式检索bean(在普通类中)?例如,以编程方式检索Bean
代码片段看起来像这样。 在此先感谢。
public class Foo {
public Foo(){
// get MyObj bean here
}
}
public class Var {
public void varMethod(){
Foo foo = new Foo();
}
}
尝试'@ Autowire'ing ...或更准确地说'@Qualifier(“myObj”)'。 – 2014-09-10 22:10:30
我不能这样做@Autowire,因为我必须在运行时使用new创建Foo对象 – user800799 2014-09-10 22:11:29
查看[Qualifier注释](http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference /html/beans.html#beans-autowired-annotation-qualifiers)。 – 2014-09-10 22:12:27