Spring Kernel studies-set method injection and constructor injection __spring

Source: Internet
Author: User

The spring species provides 2 commonly used injection modes, set method injection and constructor injection. Since these 2 injection methods are similar, they can meet our needs, so in most cases we ignore the difference of the 2 injection methods. Let's look at the characteristics of these 2 types of injection.

Let's take a look at how Spring uses the Set method injection to instantiate a bean and Bean's collaborators:

There is a Setb method in a to receive an instance of a B object. So the process of spring instantiating a object is as follows:

Without considering the initialization method of the bean and some spring callbacks, spring first invokes the constructor of a object, instantiates a, and then finds the object of a dependency, which is B (the partner) in this example. Once a partner is found, spring invokes the constructor of the partner (b) to instantiate B. If B still has dependent object spring will instantiate all objects on B with the same mechanism and then call SETB (b b) of object A to inject the B object to a.
This object must be instantiated before spring invokes the set method injection of an object. So in the case of "inject with Set method" Spring will first call the object's constructor.
We're looking at the process of injecting through a constructor:

If you find that the constructed injection of the object is configured, spring will instantiate the dependent objects required by the constructor before calling the constructor, and then call the constructor with the instantiated object as a parameter.
When using constructors and set methods to rely on injection, spring handles the order of objects and objects dependent on objects differently. In general, when a bean is designed to be a constructor to receive dependent objects, it expresses the relationship that I do not exist when they (dependent objects) do not exist, that is, "without them, without me".
In fact, a strong aggregation relationship between 2 objects is expressed through the method of injection of constructors: combinatorial relationships. Just like a car without wheels, engine and other parts then the car will not exist. And the car is made up of a number of important parts, in the absence of these components can not get off. Here the car and his important parts are combined in a relationship. If you have such a similar scenario in your application, you should manage their relationships using the "constructor injection" approach. "Constructor injection" ensures that the collaborators are created first and then create themselves.
The set method is injected in such a way that it expresses a weaker dependency between 2 objects: the aggregation relationship. It's like a car that can work without a car. Set method injection is appropriate when you do not require a partner to be created.
Although theoretically "constructor injection" and "set Method injection" represent 2 different dependency intensities, spring does not pass invalid collaborators to a bean in spring. If the partner is invalid or no spring throws an exception, spring guarantees that the collaborators of an object are available. So the only difference between "constructor injection" and "set method injection" in spring is that there are 2 ways to create collaborators in different order.

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.