7.28.1 Spring construct injection or set injection

Source: Internet
Author: User

1.

    construction Method injection code is as follows: public Usermanagerimpl (Userdao Userdao) {
                                                     this.userdao = Userdao;
                                      }

 

   set inject code as follows:         public void Setuserdao (Userdao userdao) {
                                                      this.userdao = UserDao;
                                       }     

The difference between the construction method injection and the set injection: the way to construct the method injected, the Userdao object is passed to Usermanagerimpl, the new object, at the same time as the new Usermanagerimpl object, it is very early.

Construct method Injection: The Set method is called before the Usermanagerimpl is created.

2.

Reasons for using construction method injection:

N constructor method injection uses strong dependency rules, and objects cannot be created if sufficient parameters are not given.

n because the bean's dependencies are set by the constructor method, there is no need to write more set methods to help reduce the amount of code.

Reasons to use Set injection:

n If the bean has a lot of dependencies, then the parameter list of the constructor method becomes very long.

N If an object has multiple construction methods, the construction method will cause the code to increase in volume.

n If there are more than two parameter types in the constructor method, it is difficult to determine the purpose of the parameter.

N......

Spring officially recommends using set injection.

3.

Here are some introductions and comparison of the two ways that spring relies on injection. The two ways spring relies on injection are:

3.1. Set injection;

3.2. Construction injection;

The benefits of spring-dependent injection of settings:

More similar to the traditional JavaBean, the programmer is more easy to understand and accept, and it is more intuitive and obvious to set up the dependency relationship through setter mode.

For complex dependencies, if you use construct injection, it can cause the constructor to be too bloated and difficult to read. When you create a bean instance, spring needs to instantiate all of its dependent instances at the same time, resulting in a decline in your functionality. Instead, use the set injection to avoid the problem;

In particular, when some properties are optional, a multi-parameter constructor is more awkward.

The advantages of spring-dependent injected constructs:

Construct injection can be used to determine the injection order of dependencies in the constructor and prioritize the priority injection.

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

Dependencies can only be set in the constructor, only the creator of the component can change the dependencies of the components. For the component callers, the dependencies within the components are completely transparent and conform to the high cohesion principle;

It is suggested that the injection strategy should be based on setting injection and auxiliary structure injection. As far as possible, there is no need to change the dependency of injection, the use of construction injection, while the other dependency of injection, then consider the introduction of set injection.

Landlord you say the private variables are also called State, and the method is called behavior;
As you know, a private variable of a class cannot be accessed outside of the class, that is, the private variable cannot be modified directly;
When constructing an object, some states are required, or states that are not modified in the object's life cycle, which are more convenient and safer to initialize when constructing the object (no other method can be changed, only initialized in the constructor, or Setxxx () is private);
Some states are optional, or temporary, and the object is modifiable in its life cycle, so a change method (Setxxx ()) is required to modify its value;

For example, when designing a person class, if you require a social security number, the name and gender are required to be initialized and will not change, and the address, e-mail, telephone number, etc. are optional; how do you think the best design is?

Source: http://blog.sina.com.cn/s/blog_6f93edb60101gxct.html

7.28.1 Spring construct injection or set injection

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: info-contact@alibabacloud.com 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.