简体   繁体   English

春季二传手注射不是强制性的

[英]Spring setter injection not mandatory

I read that There is no guarantee in a setter injection that the dependency will be injected as opposed to constructor injection that the dependency is mandatory. 我读到,在setter注入中不能保证将依赖项注入,而不是在构造函数注入中必须依赖。

I really don't understand that point. 我真的不明白这一点。 If I write the following method: 如果我写以下方法:

@Autowired
public void setMyBean(MyBean otherBean){
    this.otherBean = otherBean;
}

what does it mean that otherBean won't be injected? 不会注入otherBean是什么意思?

DI exists in two major variants: Constructor-based dependency injection and Setter-based dependency injection . DI存在两个主要变体: 基于构造函数的依赖注入基于Setter的依赖注入

Constructor-based Dependency Injection 基于构造函数的依赖注入

Constructor-based DI is accomplished by the container invoking a constructor with a number of arguments, each representing a dependency. 基于构造函数的DI是通过容器调用具有多个参数的构造函数来完成的,每个参数表示一个依赖项。

Setter-based Dependency Injection 基于Setter的依赖注入

Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or a no-argument static factory method to instantiate your bean. 通过调用无参数构造函数或无参数静态工厂方法来实例化您的bean之后,容器通过在bean上调用setter方法来完成基于setter的DI。

Again, constructor injection ensures all mandatory properties have been satisfied, and it is simply not possible to instantiate an object in an invalid state (not having passed its collaborators). 同样,构造函数注入可确保满足所有强制属性,并且根本不可能以无效状态(未通过其协作者)实例化对象。 In other words, when using constructor injection you do not have to use a dedicated mechanism to ensure required properties are set (other than normal Java mechanisms). 换句话说,在使用构造函数注入时,您不必使用专用机制来确保设置了必需的属性(普通的Java机制除外)。

But in setter based injection if the dependencies is not found the object is created, but the dependent object will be null. 但是在基于setter的注入中,如果找不到依赖项,则创建对象,但是依赖项对象将为null。 Means, setter injection does not ensures dependency Injection. 意味着,setter注入不能确保依赖注入。 You can find a detailed article here . 您可以在此处找到详细的文章。

Note that use of the @Required annotation on a setter method can be used to make the property be a required dependency. 请注意,在setter方法上使用@Required批注可用于使该属性成为必需的依赖项。

if you are using setter based injection in your bean then your bean will initialise no matter all dependencies have been resolved or not, but you will get NPE when try to use these non resolved/initialised dependencies in your code. 如果您在bean中使用基于setter的注入,那么无论是否已解决所有依赖项,bean都将初始化,但是当尝试在代码中使用这些未解决/初始化的依赖项时,您将获得NPE。 But in Constructor based injection your bean will initialise once all dependencies have been resolved. 但是在基于构造函数的注入中,一旦解决了所有依赖关系,您的bean将初始化。

Whenever we use @Autowired , Spring makes sure the relevant bean exists and gets injected for use. 每当我们使用@AutowiredSpring都会确保相关的bean存在并被注入使用。 If this cannot be done, Spring throws an exception and the application fails to start. 如果无法做到这一点, Spring将引发异常,应用程序将无法启动。
The statement actually is in reference to testing your code. 该语句实际上是在测试您的代码。 By this I mean, If using setter based dependency injection it may be possible that one will forget to inject the required bean and the corresponding test cases might fail. 我的意思是,如果使用基于setter的依赖项注入,则可能会忘记注入所需的bean,并且相应的测试用例可能会失败。

constructor based dependency injection ensures that one has to init all required beans before actually using the required code. 基于构造函数的依赖注入确保了在实际使用所需代码之前必须init所有必需的bean。

You can read it in detail over here 您可以在这里详细阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM