Spring依赖注入三种方式的对比

1,943 阅读2分钟

前言

由IDEA编码提示Field injection is not recommended引发的思考

依赖注入的三种方式

  • 变量注入
  • 构造器注入
  • set方法注入

变量注入

@Autowired
private CoffeeRepository coffeeRepository;

实际上是通过反射实现注入。

缺点:

  • 不允许不可变(immutable)对象的声明
  • 使得对应类无法脱离依赖注入容器进行运行
  • 使用单元测试有困难。

构造器注入

private final CoffeeRepository coffeeRepository;

public CoffeeService(CoffeeRepository coffeeRepository) {
    this.coffeeRepository = coffeeRepository;
}

Setter方法注入

private CoffeeRepository coffeeRepository;

@Autowired
public void setCoffeeRepository(CoffeeRepository coffeeRepository) {
    this.coffeeRepository = coffeeRepository;
}

构造器注入 vs setter方法注入

以下引自官方文档

Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to use constructors for mandatory dependencies and setter methods or configuration methods for optional dependencies. Note that use of the @Required annotation on a setter method can be used to make the property be a required dependency; however, constructor injection with programmatic validation of arguments is preferable.

The Spring team generally advocates constructor injection, as it lets you implement application components as immutable objects and ensures that required dependencies are not null. Furthermore, constructor-injected components are always returned to the client (calling) code in a fully initialized state. As a side note, a large number of constructor arguments is a bad code smell, implying that the class likely has too many responsibilities and should be refactored to better address proper separation of concerns.

Setter injection should primarily only be used for optional dependencies that can be assigned reasonable default values within the class. Otherwise, not-null checks must be performed everywhere the code uses the dependency. One benefit of setter injection is that setter methods make objects of that class amenable to reconfiguration or re-injection later. Management through JMX MBeans is therefore a compelling use case for setter injection.

Use the DI style that makes the most sense for a particular class. Sometimes, when dealing with third-party classes for which you do not have the source, the choice is made for you. For example, if a third-party class does not expose any setter methods, then constructor injection may be the only available form of DI.

由上文可知:

  • 构造器初始化为强制性依赖。而setter方法为可选依赖(即可能为null)。
  • 使用构造器注入可以发现编码不合适的地方,当构造器的参数过多时,就可以考虑重构拆分此类。

总结

  • 对于必须依赖或不可变对象,使用构造器注入。
  • 对于可选依赖或可变对象,使用setter注入。
  • 避免使用字段注入。

参考