问题描述
我知道有3种使用Spring进行依赖注入的方法:字段,设置器和构造函数注入。
但是说我们在同一个组件中有更多的三个,就像这样:
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'
解决方法
对于初学者来说,不要那样做。
话虽如此,以下将按顺序发生:
- 它将使用
FortuneService
实例调用构造函数,因为在任何其他事情发生之前,首先需要构造该对象。 - 它将使用
@Autowired
的实例注入用FortuneService
注释的字段 - 它将调用带有
@Autowired
实例的FortuneService
注释的方法
现在取决于FortuneService
的范围,它将注入单例(默认值)或创建一个新实例(当bean处于原型范围内时)。
注意:可以从source的AutowiredAnnotationBeanPostProcessor
中推断出排序。调用构造函数是合乎逻辑的,但是字段与方法的顺序来自buildAutowiringMetadata
方法。首先检测字段,然后检测方法。