Spring Injection method

Source: Internet
Author: User

The Spring Framework is a layered architecture consisting of 7 well-defined modules. The Spring module is built on top of the core container, and the core container defines how the beans are created, configured, and managed. Here are three ways to interface injection in spring.

Type1 Interface Injection

We often use interfaces to separate callers from their implementations. Such as:

 Public class ClassA {   private  interfaceb clzb;      Public init () {  =class.forname (config.bimplementation). newinstance ();      = (INTERFACEB) obj;   } ...   }

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 code and assign Clzb.

In this way, ClassA is dependent on the implementation of INTERFACEB during the compilation period. In order to separate the caller from the implementation at compile time, we have the above code, we dynamically load the implementation class based on the class name of the implementation class set up in the configuration file, and then use INTERFACEB to force the transformation to ClassA.

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

For a Type1 IOC container, the task of loading an interface to implement and create its instance is done by the container, such as Context.lookup (SERVLETCONTEXT.GETXXX), which is commonly used in the development of the Java EE, and is a representation of the Type1 IOC.

Apache Avalon is a typical Type1-type IOC container.

Type2 Construction sub-injection

Constructs a sub-injection, that is, through the constructor to complete the dependencies of the setting, such as:

1  Public classDibyconstructor2 {  3  Private FinalDataSource DataSource; 4  Private FinalString message; 5   PublicDibyconstructor (DataSource ds, String msg)6  {   7    This. DataSource = ds; This. Message =msg; 8  }  9 ...Ten}

As you can see, in the dependency injection mechanism of the Type2 type, the dependency is built through the class constructor, and the container injects the dependencies it needs by invoking the constructor of the class.

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

Type3 Set Value Injection

In various types of dependency injection patterns, the value injection pattern is most widely used in real-world development (a large part of which is effective in the spring framework).

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

A comparative summary of several dependency injection patterns

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.

Benefits of Type2 Construction sub-injection:

1, "in the construction period is to create a complete, legitimate object", for this Java design principle, Type2 is undoubtedly the best responder.

2, avoid the tedious setter method of writing, all dependencies are set in the constructor, the dependency relationship is concentrated, more readable.

3, because there is no setter method, the dependency relationship is set by the container at the time of construction, so the component is relatively "unchanged" after being created, so there is no need to worry about the upper-layer code executing the setter method in the call process to destroy the component dependencies. This can have a significant impact on the entire system, especially for singleton mode components.

4. Similarly, because association relationships are expressed only in constructors, only the component creator needs to be concerned about dependencies within the component. For callers, the dependencies in the component are in a black box. The unnecessary information on the upper layer shielding is also a guarantee for the hierarchy clarity of the system.

5, through the construction of sub-injection, it means that we can determine the dependency injection sequence in the constructor, for a large number of components dependent on the external service, the order of dependency can be very important, such as the prerequisite for a dependency injection is the DataSource of the component and the related resources have been set.

Type3 the benefits of setting value injection

1. For programmers accustomed to traditional javabean development, it is more intuitive and more natural to set up dependency relationship by setter method.

2, if the dependency (or inheritance relationship) is more complex, then the Type2 mode constructor will be quite large (we need to set all the dependencies in the constructor), when the Type3 pattern is often more concise.

3. For some third-party libraries, our components may be required to provide a default constructor (such as action in Struts), at which point the dependency injection mechanism of the TYPE2 type manifests its limitations and makes it difficult to accomplish the functions we expect.

It can be seen that the Type2 and Type3 modes are different, while spring and Picocontainer provide good support for the dependency injection mechanism of Type2 and Type3 types. This also gives us more options. Theoretically, the Type2 type, supplemented by the Type3 type mechanism, can achieve the best dependency injection effect, but Type3 is more widely used for applications based on spring framework development.

Spring Injection method

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.