【发布时间】:2020-12-28 17:45:23
【问题描述】:
我知道使用 Spring 进行依赖注入有 3 种方法:字段、setter 和构造函数注入。
但是假设我们在同一个组件中有更多的所有 3 个,就像这样:
import base.service.FortuneService;
@Component
public class FootballCoach implements Coach {
//Field Injection
@Autowired
private FortuneService fortuneService;
//setter Injection
@Autowired
public void setFortuneService(FortuneService fortuneService) {
this.fortuneService = fortuneService;
}
//constructor Injection
@Autowired
public FootballCoach(FortuneService fortuneService) {
this.fortuneService = fortuneService;
}
}
哪个优先 - 可以这么说? Spring 会做所有 3 次并覆盖 fortuneService 字段两次吗?如果是这样,最后一个站着的是哪一个?还是只会选择一个依赖注入?
我运行上面的代码没有问题,我得到了以下日志,但我真的不知道如何阅读它们。
注意:FortuneService 是一个接口,我有一个实现它的HappyFortuneService 类。
Sep 10, 2020 11:40:44 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry getSingleton
FINE: Creating shared instance of singleton bean 'footballCoach'
Sep 10, 2020 11:40:44 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry getSingleton
FINE: Creating shared instance of singleton bean 'happyFortuneService'
Sep 10, 2020 11:40:44 AM org.springframework.beans.factory.support.ConstructorResolver createArgumentArray
FINE: Autowiring by type from bean name 'footballCoach' via constructor to bean named 'happyFortuneService'
Sep 10, 2020 11:40:44 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry getSingleton
FINE: Creating shared instance of singleton bean 'tennisCoach'
Sep 10, 2020 11:40:44 AM org.springframework.beans.factory.support.ConstructorResolver createArgumentArray
FINE: Autowiring by type from bean name 'tennisCoach' via constructor to bean named 'happyFortuneService'
【问题讨论】:
-
构造函数必须优先于其他两个,因为你不能设置字段或调用任何方法。
-
"Spring 会做所有 3 个操作" 在 setter 中设置断点。那会给你答案。我认为是的。当然,真正的答案是“一开始就不要这样做”
-
在同一个对象的三个地方注入相同的东西是没有用的。
-
@Michael 好的,所以构造函数是第一个调用的,我可以理解,并且创建了一个 bean。但随后其他 2 个被调用。当字段和 setter 注入发生然后覆盖第一个 bean 时,是否会创建两个新 bean?如果有,按什么顺序?
-
为什么不向每个块添加
System.out.println()语句(可能将字段打印为构造函数中的第一条语句)并自己查看?
标签: java spring dependency-injection