【Spring】浅谈spring为什么推荐使用构造器注入

时间:2022-08-23 02:39:33

一、前言

​ Spring框架对Java开发的重要性不言而喻,其核心特性就是IOC(Inversion of Control, 控制反转)和AOP,平时使用最多的就是其中的IOC,我们通过将组件交由Spring的IOC容器管理,将对象的依赖关系由Spring控制,避免硬编码所造成的过度程序耦合。前几天的时候,笔者的同事问我为什么要使用构造器的注入方式,我回答说因为Spring文档推荐这种,而说不出为什么 T^T,后面抽时间了解了一下,下面就是笔者要讨论的就是其注入方式。

二、常见的三种注入方式

​ 笔者为了方便起见就只是用注解的方式注入(现在也很少使用xml了吧,(~ ̄▽ ̄)~)

2.1 field注入

@Controller
public class FooController {
@Autowired
//@Inject
private FooService fooService; //简单的使用例子,下同
public List<Foo> listFoo() {
return fooService.list();
}
}

这种注入方式应该是笔者目前为止开发中见到的最常见的注入方式。原因很简单:

  1. 注入方式非常简单:加入要注入的字段,附上@Autowired,即可完成。
  2. 使得整体代码简洁明了,看起来美观大方。

2.2 构造器注入

@Controller
public class FooController { private final FooService fooService; @Autowired
public FooController(FooService fooService) {
this.fooService = fooService;
} //使用方式上同,略
}

​ 在Spring4.x版本中推荐的注入方式就是这种,相较于上面的field注入方式而言,就显得有点难看,特别是当注入的依赖很多(5个以上)的时候,就会明显的发现代码显得很臃肿。对于从field注入转过来+有强迫症的园友 来说,简直可以说是石乐志  (`Д´*)9。对于这一点我们后面再来讨论,别急。

2.3 setter注入

@Controller
public class FooController { private FooService fooService; //使用方式上同,略
@Autowired
public void setFooService(FooService fooService) {
this.fooService = fooService;
}
}

​ 在Spring3.x刚推出的时候,推荐使用注入的就是这种,笔者现在也基本没看到过这种注解方式,写起来麻烦,当初推荐Spring自然也有他的道理,这里我们引用一下Spring当时的原话:

The Spring team generally advocates setter injection, because large numbers of constructor arguments can get unwieldy, especially when properties are optional. Setter methods also make objects of that class amenable to reconfiguration or re-injection later. Management through JMX MBeans is a compelling use case.

Some purists favor constructor-based injection. Supplying all object dependencies means that the object is always returned to client (calling) code in a totally initialized state. The disadvantage is that the object becomes less amenable to reconfiguration and re-injection.

​ 咳咳,简单的翻译一下就是:构造器注入参数太多了,显得很笨重,另外setter的方式能用让类在之后重新配置或者重新注入

​ 那么后面为什么又换成构造器注入了呢?(喂喂喂,Spring你前一大版本还贬低构造器注入,后面就立刻捧人家了不好吧,不过能用于承认自己的错误,才是真正令人称赞的地方吧 (๑•̀ㅂ•́)و✧)

三、构造器注入的好处

​ 先来看看Spring在文档里怎么说:

The Spring team generally advocates constructor injection as it enables one to implement application components as immutable objects and to ensure that required dependencies are not null. Furthermore constructor-injected components are always returned to client (calling) code in a fully initialized state.

​ 咳咳,再来简单的翻译一下:这个构造器注入的方式啊,能够保证注入的组件不可变,并且确保需要的依赖不为空。此外,构造器注入的依赖总是能够在返回客户端(组件)代码的时候保证完全初始化的状态

下面来简单的解释一下:

  • 依赖不可变:其实说的就是final关键字,这里不再多解释了。不明白的园友可以回去看看Java语法。
  • 依赖不为空(省去了我们对其检查):当要实例化FooController的时候,由于自己实现了有参数的构造函数,所以不会调用默认构造函数,那么就需要Spring容器传入所需要的参数,所以就两种情况:1、有该类型的参数->传入,OK 。2:无该类型的参数->报错。所以保证不会为空,Spring总不至于传一个null进去吧