Three ways to inject the Spring IOC

Source: Internet
Author: User

Spring IOC three ways to inject:

1. Interface Injection

2. Getter,setter Mode Injection

3. Constructor injection

The relationship between an object and an object can be easily understood as a dependency between objects:
Class A requires an instance of Class B to perform certain operations, such as the need to invoke class B methods in a Class A method to complete the function, called Class A relies on Class B.
Control inversion is a technique that puts the creation and management of component dependencies outside the program, which is the relationship between the container control program and not directly controlled by the code.

1. Interface Injection

public class ClassA {
Private Interfaceb CLZB;
public void dosomething () {
Ojbect obj = Class.forName (config.bimplementation). newinstance ();
CLZB = (interfaceb) obj;
Clzb.doit ();
}
......
}

In the above code, ClassA relies on the implementation of INTERFACEB, how to obtain an instance of the Interfaceb implementation class? The traditional approach is to create an instance of the INTERFACEB implementation class in your code and give CLZB. ClassA is dependent on the implementation of INTERFACEB at compile time. In order to separate the caller from the implementation at compile time, the above code is available.
We dynamically load the implementation class based on the class name (config.bimplementation) of the implementation class set up in the configuration file, and use it for ClassA after Interfaceb forced transformation, which is the most primitive prototype of interface injection.

public class ClassA {
Private Interfaceb CLZB;
Public Object dosomething (Interfaceb b) {
CLZB = b;
return Clzb.doit ();
}
......
}

In the above code, the task of loading an interface implementation and creating its instance is done by the container.
At run time, the INTERFACEB instance will be provided by the container. Even when the IOC concept is not yet established, such a method has frequently appeared in our code.

public class Myservlet extends HttpServlet {
public void doget (HttpServletRequest request,httpservletresponse response) throws Servletexception, IOException {
......
}
}

HttpServletRequest and HttpServletResponse instances are injected dynamically by the servlet container at run time.

2.Setter Set Injection
The dependency injection mechanism based on setup mode is more intuitive and more natural.

public class ClassA {
Private Interfaceb CLZB;
public void Setclzb (Interfaceb clzb) {
THIS.CLZB = CLZB;
}
......
}

3. Constructor injection

public class Dibyconstructor {
Private final DataSource DataSource;
Public Dibyconstructor (DataSource DS) {
This.datasource = ds;
}
......
}

constructor injection, that is, through the constructor to complete the dependency of the setting, the container by invoking the constructor method of the class to inject its required dependencies.

Comparison of three injection methods:

Interface injection:

Interface injection mode because it is intrusive, it requires that the component must be associated with a specific interface, so it is not optimistic, the actual use is limited.

Setter Injection:

For programmers accustomed to the development of traditional javabean, it is more intuitive to set the dependency relationship by setter method.

If the dependencies are more complex, then the constructors for constructing the sub-injection pattern will be quite large, while the value injection pattern is more concise.

If a third-party class library is used, our component may be required to provide a default constructor, and the construction sub-injection mode does not apply at this time.

Constructor injection:

Complete a complete, legitimate object during construction.

All dependencies are rendered centrally in the constructor.

Dependencies are set at the time of construction by the container and remain in a relatively "constant" state after the component is created.

Only the creator of the component cares about its internal dependencies, and for the caller, the dependency is in the black box.

Summarize

Spring uses injection, why the use of injection, this series of questions actually boils down to a sentence, spring injection and the IOC (I say about the IOC) inversion control is one thing.

Theoretically: The third injection method (constructor injection) is more reasonable in accordance with Java usage, and the second method of injection (setter injection) complements.

In fact: I personally think that the second injection method (setter injection) can achieve more intuitive effect, in the use of work has incomparable advantages, so setter injection dependency application more extensive.

Three ways to inject the Spring 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.