Spring's IOC container injection

Source: Internet
Author: User

An introduction to the principles of IOC and AOP is presented in this article, which focuses on the injection of IOC. But I'm still puzzled by a question:

One: There are three kinds of injection methods in dependency injection (DI), how many ways does spring implement? is also three kinds?

IOC is implemented in many frameworks, not spring-specific, previously said that IOC mainly contains DL (Dependency Lookup) and Di (Dependency injection), which means that there are many techniques to implement IOC, But the main include di and DL, but relatively speaking, Di is widely used, I think this is why Martin Fowler will control the reverse switching dependency injection to replace the reason, in the specific application Di mainly implemented interface injection (Type1 IOC), Setter Injection (Type2 IOC), constructor injection (Type3 IOC) in three ways, in the spring IOC design, the implementation of value injection and construction injection, but also the most important injection method (that is, there are other injection methods in spring), There seems to be no way to implement interface injection at this time, but in another framework Avalon is implemented.

In other words: 1. Dependency Injection (DI) has three injection methods: interface Injection (Type1 IOC), SetPoint injection (Type2 IOC), Construction injection (Type3 IOC)

2. In spring, the IOC is primarily a DI implementation, DI implements value injection and construction injection, but there are other ways to inject the spring IOC in addition to dependency injection.

3. The IOC implementations of all Java EE frameworks, mainly on DI-based Interface injection (Type1 IOC), Setter injection (Type2 IOC), constructor injection (Type3 IOC), are implemented in three ways


So since the IOC in spring is implemented primarily with value injection and construct injection implementations, these two types are implemented first:

Set value injection (setter injection):

<!--uses the SetPoint injection method--><bean id= "Examplebean" class= "examples. Examplebean ">  <!--setter injection using the nested <ref/> element--><property name=" Beanone " ><ref bean= "Anotherexamplebean"/></property> <!--setter injection using the neater ' ref ' attribute-- ><property name= "Beantwo" ref= "Yetanotherbean"/><property name= "Integerproperty" value= "1"/></ Bean><bean id= "Anotherexamplebean" class= "examples. Anotherbean "/><bean id=" Yetanotherbean "class=" examples. Yetanotherbean "/>


Package Examples;public class Examplebean {private Anotherbean beanone;//First Java beanprivate Yetanotherbean beantwo;// Two Java beanprivate int i;//one shaping variable public void Setbeanone (Anotherbean beanone) {this.beanone = Beanone;} public void Setbeantwo (Yetanotherbean beantwo) {this.beantwo = Beantwo;} public void Setintegerproperty (int i) {this.i = i;}}

Construction Injection:

<!--uses the construction injection method--><bean id= "Examplebean" class= "examples. Examplebean "><!--constructor injection using the nested <ref/> element--><constructor-arg>< ref= "Anotherexamplebean"/></constructor-arg><!--constructor injection using the neater ' ref ' attribute-- ><constructor-arg ref= "Yetanotherbean"/><constructor-arg type= "int" value= "1"/></bean>< Bean id= "Anotherexamplebean" class= "examples. Anotherbean "/><bean id=" Yetanotherbean "class=" examples. Yetanotherbean "/>


Package Examples;public class Examplebean {private Anotherbean beanone;//First Java beanprivate Yetanotherbean beantwo;// Two Java beanprivate int i;//One shaping variable public examplebean (        anotherbean Anotherbean, Yetanotherbean yetanotherbean, int i) {        this.beanone = Anotherbean;        This.beantwo = Yetanotherbean;        this.i = i;    }}

In addition to these two, you can also inject using static factory methods and instance factory methods

Static Factory Method:

-----------We can use other methods to return object instances in addition to using Di for---------------<!--Returning an object instance using a static factory method--><bean id= "Examplebean" class= "examples. Examplebean "factory-method=" CreateInstance "><constructor-arg ref=" Anotherexamplebean "/>< Constructor-arg ref= "Yetanotherbean"/><constructor-arg value= "1" ></bean><bean id= " Anotherexamplebean "class=" examples. Anotherbean "/><bean id=" Yetanotherbean "class=" examples. Yetanotherbean "/>

public class Examplebean {    //a private constructor    private Examplebean (...) {      ...    }        A static factory method; The arguments to this method can be    //considered the dependencies of the beans is returned,    //regardless of How those arguments is actually used.    public static Examplebean CreateInstance (            anotherbean Anotherbean, Yetanotherbean yetanotherbean, int i) {        Examplebean EB = new Examplebean (...);        Some other operations        ... return EB;    }}

Instance Factory Method:

<!--instantiate the--><bean id= "Examplebean" factory-bean= "Instancefactory" factory-method= by using the instance factory method ><constructor-arg ref= "Anotherexamplebean"/><constructor-arg ref= "Yetanotherbean"/>< Constructor-arg value= "1" ></bean><bean id= "instancefactory" class= "Examples.examplebean"/><bean Id= "Anotherexamplebean" class= "examples. Anotherbean "/><bean id=" Yetanotherbean "class=" examples. Yetanotherbean "/>

public class Examplebean {    //a private constructor    private Examplebean (...) {      ...    }        Public Examplebean CreateInstance (            anotherbean Anotherbean, Yetanotherbean yetanotherbean, int i) {        Examplebean EB = new Examplebean (...);        Some other operations        ... return EB;    }}
Please note that the transmission to StaticThe parameters of the factory method are determined byConstructor-argelement, which is exactly the same as when using a constructor injection. Also, it is important that the type of instance returned by the factory method does not necessarily relate to the containingStaticThe class type of the factory method is the same. In this case, though, it does. The non-static instance factory method is the same as this (in addition to usingFactory-beanProperty OverridesclassProperties).

Reference: Spring Technology Insider, "Dependency injection"

Spring Official Handbook: http://docs.spring.io/spring/docs/

Welcome to explore Spring technology together, there will be time to continue to update, I hope to continue to improve, the error is very welcome.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring's IOC container 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.