我有一个演示类 FooComponent
,它在 FooService
中 Autowiring 并在其构造函数中访问。
FooComponent.class:
@Component("fooComponent")
public class FooComponent {
public String format() {
return "foo";
}
}
FooService.class:
@Component
public class FooService {
@Autowired
private FooComponent fooComponent;
public FooService() {
System.out.println("in foo service const=" + this.fooComponent);
}
String doTatti() {
return fooComponent.format();
}
}
主要应用:
@SpringBootApplication
public class InterviewApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(InterviewApplication.class, args);
System.out.println(ctx.getBean(FooService.class).doTatti());
}
}
在此tutorial ,作者说在上面的示例中,Spring 在创建 FooService 时查找并注入(inject) fooFormatter。
在我的例子中,fooFormatter 是 fooComponent。
但是每次,在构造函数中我的 Autowiring 属性都是空的。我的假设是,这是因为 FoOService
还没有完全初始化?这是正确的吗?
如果我的假设是正确的,那么为什么下面的代码可以工作?
@Autowired
public FooService(FooComponent fooComponent) {
System.out.println("in foo service const=" + fooComponent);
}
我知道这是非常基本且愚蠢的问题,但我需要帮助来理解它。
更新:
最后一个问题,有没有办法在 MainApplication
中Autowire
我的 FooService
实例,而不是从 ApplicationContext< 获取它
?
提前谢谢您。
最佳答案
在 Spring 中,bean 在其属性被注入(inject)之前被实例化。即:
- 首先实例化 bean
- 注入(inject)属性 这是因为 spring 使用实例化 bean 的 setter 方法来注入(inject)属性。
这就是您收到 NullPointerException 的原因。
首选和推荐的选项是使用构造函数注入(inject),而不是属性或 setter 注入(inject)。此外,解决此问题的方法之一是像您一样创建带参数的构造函数。
您可以在这里查看完整的解释: https://dzone.com/articles/spring-accessing-injected
编辑: Bean 应该由容器管理。如果我们想使用其中之一,我们应该依赖依赖注入(inject)而不是直接调用ApplicationContext.getBean()。
查看这篇文章:Why is Spring's ApplicationContext.getBean considered bad?
关于java - 当我在构造函数中调用 Autowired 属性时,它为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61029655/