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 ways of Spring IOC injection?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge points about Spring IOC injection, which are detailed in content and clear in logic. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article.

There are three injection methods for Spring IOC:

1. Interface injection

2. Getter,setter injection

3. Constructor injection

The relationship between objects can be simply understood as the dependencies between objects.

Class A needs an instance of Class B to perform certain operations, such as calling the method of Class B in the method of Class A to complete the function, which is called Class A depends on Class B.

Control inversion is a technique that puts the creation and management of component dependencies outside the program. The relationship between programs is controlled by the container, not directly 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 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. 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, there is the above code.

According to the class name (Config.BImplementation) of the implementation class set in the configuration file, we dynamically load the implementation class and force the transformation through InterfaceB to be used by ClassA, which is one of the most primitive prototypes of interface injection.

Public class ClassA {

Private InterfaceB clzB

Public Object doSomething (InterfaceB b) {

ClzB = b

Return clzB.doIt ()

}

……

}

In the above code, the work of loading the interface implementation and creating its instance is done by the container.

At run time, the InterfaceB instance will be provided by the container. Even before the concept of IOC was established, such an approach appeared frequently in our code.

Public class MyServlet extends HttpServlet {

Public void doGet (HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

……

}

}

HttpServletRequest and HttpServletResponse instances are dynamically injected by Servlet Container at run time.

2.Setter setting injection

The dependency injection mechanism based on setting mode is more intuitive and 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, the dependency is set through the constructor, and the container injects the required dependency into it by calling the constructor of the class.

Comparison of three injection methods:

Interface injection:

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.

Setter injection:

For programmers accustomed to traditional javabean development, it is more intuitive to set dependencies through the setter method.

If the dependency relationship is more complex, then the constructor of the sub-injection pattern will also be quite large, while setting 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 constructor subinjection pattern is not applicable.

Constructor injection:

Complete a complete and legal object during construction.

All dependencies are presented centrally in the constructor.

The dependency is set by the container at one time when it is constructed, and the component has been in a relatively "constant" stable state since it was created.

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

These are all the contents of the article "what are the ways to inject Spring IOC?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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