The way to inject spring learning

Source: Internet
Author: User

We know that spring object properties are injected in two ways: Set value injection and construct injection.
Suppose you have a class of people that contains three properties, name and school, and age, which have their own setter and getter methods, and a constructor that contains the three properties. If you use spring to manage this object, there are two ways to set properties for people:
(1) Set Value injection

1 <BeanID= "People"class= "">2      < Propertyname= "Name"value= "Zhang San" /> <!--Set Value injection -3      < Propertyname= "School"ref= "School" /> <!--Set Value injection -4      < Propertyname= "Age"value= " the"type= "int" />5 </Bean>6 <BeanID= "School"class= "" />

(2) Construction injection

1 <BeanID= "People"class= "">2     <!--construct injection, index=0 represents the first argument of a constructor -3      <Constructor-argIndex= "0"value= "Zhang San"/> 4      <Constructor-argIndex= "1"ref= "School" /> <!--Construction Injection -5      <Constructor-argIndex= "2"value= " the"type= "int" />6 </Bean>7 <BeanID= "School"class= "" />

So, what is the difference between these two injections? A simple comparison is made below:
In the past development process, both of these injection methods are very common, spring also supports two types of dependency injection: Value injection and construction injection, the two methods of dependency injection, and not absolutely good or bad, just adapt to the scene is different. In contrast, setting value injection has the following advantages:

    • setting value injection requires the bean to contain the setter method for these properties

    • Span style= "font-family: italics; font-size:16px; " It is much more similar to the traditional JavaBean, which makes it easier for program developers to understand and receive, and it is more intuitive to set up dependency relationships through setter methods.

    • Construction injection is not as good as setting value injection, and in some specific scenarios, construction injection is better than setpoint injection. Construction injection has the following advantages:

    • Construction injection has the following advantages :
    • Construction injection requires that the bean contain a constructor with these attributes

    • Construction injection can determine the injection order of dependencies in the constructor, prioritizing the priority injection of dependency. For example, the injection of other dependencies in a component is often dependent on datasrouce injection. With construction injection, you can clearly determine the injection sequence in your code.

    • Construction injection is more useful for beans that do not need to change dependencies because there is no setter method and all dependencies are set within the constructor. Therefore, there is no need to worry about subsequent code corruption of the dependency relationship.

    • Dependencies can only be set in the constructor, only the creator of the component can change the dependencies of the components. For the caller of the component, the dependencies inside the component are completely transparent and more consistent with the high cohesion principle.

  Recommendation: The injection strategy is based on the value injection, and the construction injection as a supplement. As far as the dependency relationship does not require a change of injection, the construction injection is used as much as possible, while the other dependency injection takes the value injection into consideration.

The way to inject spring learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.