chimeraspring

spring为何推荐使用构造器注入

2018-12-26  本文已影响0人  _大橙子_

在idea中使用field注入时,提示

Field injection is not recommended.

Inspection info: Spring Team recommends: "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies"

spring团队为何推荐使用构造器注入,而不推荐方法注入呢?
下面将对常见的三种构造器注入方式进行介绍。

filed注入

使用方法

@Controllerpublic classFooController{
    @Autowired  // @Inject  
    private FooService fooService;
    //简单的使用例子,下同  
    publicListlistFoo(){
      return fooService.list();
    }
}

该种注入方式是使用开发中最常见的注入方式。

优点

  1. 注入方式简单:加入要注入的字段,附上注解@AutoWired
  2. 整体代码简洁明了

缺点

  1. 对于IOC容器以外的环境,除了使用反射来提供它需要的依赖之外,无法复用该实现类。而且将一直是个潜在的隐患,因为你不调用将一直无法发现NullPointException的存在
  2. 使用field注入可能会导致循环依赖
public class A {
    @Autowired
    private B b;
}

public class B {
    @Autowired
    private A a;
}

constructor注入

使用方法

@Controller
public class FooController {  
    private final FooService fooService;
  
    @Autowired
    public FooController(FooService fooService) {
        this.fooService = fooService;
    }
}

优点

缺点

setter注入

使用方法

@Controller
public class FooController {
    private FooService fooService;

    @Autowired
    public void setFooService(FooService fooService) {
        this.fooService = fooService;
    }
}

优点

  1. 相比构造器注入,当注入参数太多或存在非必须注入的参数时,不会显得太笨重
  2. 允许在类构造完成后重新注入

缺点

答疑

Q1:如果有大量的依赖要注入,构造方法注入不会显得很臃肿吗?

对于这个问题,说明你的类当中有太多的责任,那么你要好好想一想是不是自己违反了类的单一性职责原则,从而导致有这么多的依赖要注入。

Q2:是不是其他的注入方式都不适合用了呢?

当然不是,存在即是合理!setter的方式既然一开始被Spring推荐肯定是有它的道理,像之前提到的setter的方式能用让类在之后重新配置或者重新注入,就是其优点之一。除此之外,如果一个依赖有多种实现方式,我们可以使用@Qualifier,在构造方法里选择对应的名字注入,也可以使用field或者setter的方式来手动配置要注入的实现。

转载: http://www.cnblogs.com/joemsu/p/7688307.html

上一篇 下一篇

猜你喜欢

热点阅读