【发布时间】:2014-11-04 17:17:17
【问题描述】:
@Configuration
public class MyConfig {
@Bean(name = "myObj")
public MyObj getMyObj() {
return new MyObj();
}
}
我有这个带有 @Configuration Spring 注释的 MyConfig 对象。 我的问题是如何以编程方式(在普通课程中)检索 bean?
例如,代码 sn-p 看起来像这样。 提前致谢。
public class Foo {
public Foo(){
// get MyObj bean here
}
}
public class Var {
public void varMethod(){
Foo foo = new Foo();
}
}
【问题讨论】:
-
试试
@Autowireing...或更准确地说是@Qualifier("myObj")。 -
我不能做 @Autowire 因为我必须在运行时使用 new 创建 Foo 对象
-
相关:Spring injecting or autowiring datasource bean to class。检查我答案底部的部分。
-
你是否在你的 Spring 上下文文件中设置了它?