Types of IoC

Source: Internet
Author: User

From the injection method, the main can be divided into three types: constructor injection, attribute injection and interface injection. Spring supports constructor injection and attribute injection. Let's continue using the examples above to illustrate the differences between the three injection methods.

  1. Constructor injection
In constructor injection, we pass an interface implementation class through a constructor variable by invoking the constructor of the class, as shown in Listing 3-3:

  Code Listing 3-3 Moattack: Injecting leather from the player with a constructor function

 Public class moattack {    private  GeLi GeLi;      // ① The specific actor who injected the leather from the     Public Moattack (GeLi GeLi) {        this. GeLi = GeLi;    }       Public void Citygateask () {        geli.responseask ("ink-free! ");    }}

Moattack's constructor does not care about who plays the role of the ①, as long as the players who are introduced to the play are required to perform the corresponding performances according to the script. The specific player of the role is arranged by the director, as shown in Listing 3-4:

  Code Listing 3-4 Director: Injecting leather from the player with a constructor function

 Public class Director {    publicvoid  Direct () {        //① The actor who specified the role        New  Liudehua ();         // ② Inject concrete actors into the play        New Moattack (Geli);        Moattack.citygateask ();    }}

In ①, the director arranges Andy Lau to play the role of the ②, and in the place, Andy Lau "injected" into the script of the Mexican attack, and then began the "gate inquiry" drama performance work.

  2. Attribute Injection
Sometimes, the director will find, although the film "Ink Attack" the first protagonist, but not every scene requires the appearance of leather, in this case through the construction function injection is not appropriate, you can consider using attribute injection. Attribute injection can optionally be used to complete the injection of the invocation class by the setter method, which is more flexible and convenient:

  Code Listing 3-5 Moattack: Using setter method to inject leather from player

 Public class moattack {    private  GeLi GeLi;     // ① Attribute Injection method     Public void Setgeli (GeLi GeLi) {        this. GeLi = GeLi;    }       Public void Citygateask () {        geli.responseask ("ink Off");}    }

Moattack provides a setter method for the Geli attribute at ① to allow the director to inject Geli's specific player when needed.

  Code Listing 3-6 Director: Using setter method to inject leather from player

 Public class Director {    publicvoid  Direct () {        new  Liudehua ();         New Moattack ();         // ① Call Property setter Method injection         Moattack.setgeli (geli);        Moattack.citygateask ();    }}

And through the constructor to inject the leather from the player is different, in the instantiation of the Moattack script, did not specify any player, but after the instantiation of Moattack, when the need to turn off the appearance of the Setgeli () method to inject the actor. In a similar way, we can also provide an injection setter method for other characters in the script, such as Liang, lane-flooded, etc., so that the director can inject the corresponding role according to the different part of the play.

  

  3. Interface Injection
Extracting all the methods of dependency injection of the calling class into an interface, the calling class provides the appropriate injection method by implementing the interface. In order to take the form of interface injection, you must first declare a actorarrangable interface:

 Public Interface actorarrangable {    void  Injectgeli (GeLi GeLi);}

The moattack then implements the Actorarrangable interface to provide a concrete implementation:

Code Listing 3-7 Moattack: Injecting leather from the player by interface method

  

 Public class Implements actorarrangable {    private  GeLi GeLi;     // ① Implementing interface Methods     Public void Injectgeli (GeLi GeLi) {        this. GeLi = GeLi;    }       Public void Citygateask () {        geli.responseask ("ink Off");}    }

The Director completes the injection of the player through the Actorarrangable Injectgeli () method.

Code Listing 3-8 Director: Injecting leather from the player via interface method

 Public class Director {    publicvoid  Direct () {        new  Liudehua ();         New Moattack ();        Moattack. Injectgeli (Geli);       Moattack.citygateask ();    }}

Because of the additional declaration of an interface through interface injection, the number of classes is increased, and its effect and attribute injection are not fundamentally different, so we do not advocate this approach.

Types of IoC

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.