Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the three ways of interface injection in Spring?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what are the three ways of interface injection in Spring, which are concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

There are three ways of interface injection in Spring. In the above code, ClassA depends on the implementation of InterfaceB. How to get an example of the InterfaceB implementation class? The traditional method is to create an instance of the InterfaceB implementation class in the code and assign it to clzB.

Type1 interface injection

We often use interfaces to separate callers from implementers. 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 depends on the implementation of InterfaceB, so how do you get an example of the InterfaceB implementation class? The traditional method is to create an instance of the InterfaceB implementation class in the code and assign it to clzB.

In this way, ClassA depends on the implementation of InterfaceB at compile time. In order to separate the caller from the implementer at compile time, with the above code, we dynamically load the implementation class according to the class name of the implementation class set in the configuration file, and force the transformation through InterfaceB to be used by ClassA.

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

For a Type 1 IOC container, the work of loading the interface implementation and creating its instances is done by the container, such as Context.lookup (ServletContext.getXXX), which is commonly used in J2EE development, which is the representation of Type 1 IOC.

Apache Avalon is a typical type 1 IOC container.

Type2 constructor injection

Constructor injection, that is, setting dependencies through constructors, 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 dependency injection mechanism of Type2 type, the dependency is established through the class constructor, and the container injects the required dependency into it by calling the constructor of the class.

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

Type3 set value injection

Among the various types of dependency injection patterns, the set value injection pattern is the most widely used in practical development (a large part of which is due to the influence of the Spring framework).

From the author's point of view, the dependency injection mechanism based on setting mode is more intuitive and natural. The example in Quick Start is typical setting injection, that is, setting dependencies through the setter method of the class.

Comparison and Summary of several dependency injection modes

Because the interface injection pattern is intrusive, it requires that the component must be associated with a specific interface, so it is not favored and the actual use is limited.

Advantages of Type2 constructor injection:

1. "create a complete and legal object during the construction period". For this Java design principle, Type2 is undoubtedly a responder.

2. Avoid the tedious writing of the setter method, all the dependencies are set in the constructor, and the dependencies are presented centrally, making it easier to read.

3. Because there is no setter method, the dependency is set by the container at one time during construction, so the component is in a relatively "constant" stable state after it is created, so there is no need to worry that the upper code executes the setter method during the call process to destroy the component dependency, especially for the components of Singleton mode, which may have a significant impact on the whole system.

4. Similarly, because the association is only expressed in the constructor, only the component creator needs to care about the dependencies within the component. For the caller, the dependencies in the component are in the black box. Shielding unnecessary information to the upper layer also ensures the hierarchical clarity of the system.

5. The constructor injection means that we can determine the injection order of dependencies in the constructor. For a component that relies heavily on external services, the order of dependency acquisition may be very important. For example, a prerequisite for dependency injection is that the component's DataSource and related resources have been set.

Advantages of Type3 setting injection

1. For programmers who are accustomed to traditional JavaBean development, it is more intuitive and natural to set dependencies through setter method.

2. If the dependencies (or inheritance relationships) are complex, then the constructor of the Type2 schema will also be quite large (we need to set all the dependencies in the constructor), and the Type3 schema tends to be more concise.

3. For some third-party class libraries, our components may be required to provide a default constructor (such as Action in Struts). At this time, the dependency injection mechanism of Type2 type reflects its limitations and is difficult to complete the desired function.

It can be seen that Type2 and Type3 patterns have their own advantages, while Spring and PicoContainer both provide good support for dependency injection mechanisms of Type2 and Type3 types. This also provides us with more choices. In theory, the dependency injection effect can be achieved by using Type2 type as the main type and supplemented by Type3 type mechanism, but Type3 is more widely used for applications developed based on Spring Framework.

What are the three ways of interface injection in Spring? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report