Three ways of interface injection in spring

Source: Internet
Author: User
Tags constructor

Type1 Interface Injection

We often use interfaces to separate the caller from the implementing person. Such as:

public class ClassA {
private InterfaceB clzB;
public init() {
Ojbect obj =
Class.forName(Config.BImplementation).newInstance();
clzB = (InterfaceB)obj;
}
……
}

In the above code, ClassA relies on the implementation of the 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 assign it to CLZB.

In this way, ClassA relies on the implementation of INTERFACEB at compile time. In order to separate the caller from the implementation at compile time, and with the above code, we dynamically load the implementation class according to the class name of the implementation class set in the configuration file in advance, and use the ClassA after the Interfaceb forced transformation.

This is one of the most primitive prototypes of interface injection.

For a Type1 IOC container, the work of loading an interface implementation and creating its instance is done by the container, such as Context.lookup (SERVLETCONTEXT.GETXXX), which is commonly used in the development of Java-EE, and is the manifestation of the Type1 IOC.

The Apache Avalon is a typical Type1-type IOC container.

Type2 Construction sub-injection

Constructs a child injection, that is, to complete the setting of a dependency through a constructor, such as:

public class DIByConstructor {
private final DataSource dataSource;
private final String message;
public DIByConstructor(DataSource ds, String msg) {
this.dataSource = ds;
this.message = msg;
}
……
}

As you can see, in the Type2 type of dependency injection mechanism, dependencies are established through class constructors, and containers are injected into them by invoking the constructor method of the class.

Picocontainer (another lightweight container that implements a dependency injection pattern) first implements the Type2 type Dependency injection pattern.

Type3 Set Value Injection

In various types of dependency injection patterns, the set-value injection pattern has been the most widely used in practical development (a large proportion of which is influenced by the spring framework).

In the author's opinion, the dependency injection mechanism based on setting mode is more intuitive and more natural. The example in Quick start is a typical set injection, that is, the setting of dependencies through the setter method of the class.

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.