构造函数注入的方式:
public class TestController {
private final TestService testService;
@Autowired
public TestController(TestService testService) {
this.testService = testService;
}
…
}
变量注入的方式:
public class TestController {
@Autowired
private TestService testService;
…
}
@Autowired 可以对成员变量、方法以及构造函数进行注释。那么对成员变量和构造函数进行注释又有什么区别呢?
@Autowired注入bean,相当于在配置文件中配置bean,并且使用setter注入。而对构造函数进行注释,就相当于是使用构造函数进行依赖注入。
先看一段代码,下面的代码能运行成功吗?
@Autowired private User user; private String school;
public UserAccountServiceImpl(){ this.school = user.getSchool(); } 答案是不能。
因为Java类会先执行构造方法,然后再给注解了@Autowired 的user注入值,所以在执行构造方法的时候,就会报错。
报错信息可能会像下面:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘...‘ defined in file [....class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [...]: Constructor threw exception; nested exception is java.lang.NullPointerException 1 报错信息说:创建Bean时出错,出错原因是实例化bean失败,因为bean时构造方法出错,在构造方法里抛出了空指针异常。
解决办法是,使用构造器注入,如下:
private User user; private String school;
@Autowired public UserAccountServiceImpl(User user){ this.user = user; this.school = user.getSchool();
可以看出,使用构造器注入的方法,可以明确成员变量的加载顺序。
PS:Java变量的初始化顺序为:静态变量或静态语句块–>实例变量或初始化语句块–>构造方法–>@Autowired
那么最开始Spring建议,为何要将成员变量加上final类型呢?
网上有解释如下:spring配置默认的bean的scope是singleton,也就是启动后一直有。通过设置bean的scope属性为prototype来声明该对象为动态创建。但是,如果你的service本身是singleton,注入只执行一次。
@Autowired本身就是单例模式,只会在程序启动时执行一次,即使不定义final也不会初始化第二次,所以这个final是没有意义的吧。
可能是为了防止,在程序运行的时候,又执行了一遍构造函数;
或者可能是更容易让人理解的意思吧,加上final只会在程序启动的时候初始化一次。
来源:https://blog.csdn.net/qq_28587263/article/details/75570745
https://www.cnblogs.com/joemsu/p/7688307.html