Deeply analyze the dependency injection of beans in Java’s spring framework

Each Java based application has several objects that work together to show the content to the user as a working application. When writing a complex Java application, the application class should be independent of other Java classes as much as possible to increase the possibility of reusing these classes and testing them independently of other classes. Dependency injection (or sometimes called wiring) helps glue these classes together while maintaining their independence.

Consider an application that has a text editor component that provides spell checking. The standard code will look like this:

What we're doing here is creating dependencies between text editing and spell checking. Instead of reversing the control scheme, we will do the following:

Here, text editors should not worry about spell checking implementation. The spell checker will be implemented independently and will be provided to the text editor. When the text editor is instantiated, the whole process is controlled by the spring framework.

Here, we have removed the overall control from text editing and kept it elsewhere (i.e. XML configuration file) and dependencies (i.e. class spelling check) injected into class text editing through class constructor. Therefore, process control has been "inverted" through dependency injection (DI) because it has been effectively delegated to some external systems.

The second method of dependency injection is through the text editing class. We will create the setter method of the spell check instance, which will be used to call the setter method to initialize the properties of text editing.

Therefore, di mainly has two variants and the following two subchapters will cover examples of their combination:

Constructor based dependency injection when a container calls a class's constructor with multiple parameters, each representing a di based on constructor dependencies in other classes.

Example: the following example shows that a class text editor can only be dependency injection and constructor injection.

We use the eclipse IDE, and then follow the steps below to create a spring application:

This is texteditor Contents of java file:

Here is another related class file, spellchecker Java content:

Here is mainapp Contents of java file:

The following is the configuration file beans The XML file contains the injection configured based on the constructor:

After creating the source code and bean configuration file, let's run the application. If all goes well, the following information will be printed:

Parameter resolution of constructor: there may be ambiguity, and there is more than one parameter when passing the parameter to the constructor. To solve this uncertainty, the order in which constructor parameters are defined in a bean definition is the order in which they are provided to the appropriate constructor. Consider the following classes:

The following configurations work correctly:

Let's check one more case where we pass different types of constructors. Consider the following classes:

Containers can also use type matching with simple types if you explicitly specify the parameter type of the constructor using the type attribute. For example:

Finally, through the best method of constructor parameters, the index attribute is used to explicitly specify the index of a constructor parameter. The index here starts from 0. For example:

Finally, if you pass a reference to an object, you need to use the ref attribute of the < constructor Arg > tag. If you pass a value directly, you should use the value attribute.

The dependency injection based on setter method is based on setter di. The container calls setter method to call parameterless constructor or parameterless static factory method to instantiate the bean. This is texteditor Contents of java file:

Here, you need to check the naming convention of the setter method. Set us to use the setspellchecker () method, which is very similar to the spelling checker for variables of the Java POJO class. Let's create another related class file, spellchecker Java, as follows:

Here is mainapp Contents of java file:

The following is the configuration file beans The XML file is configured to inject based on setter method:

It should be noted that beans are defined based on constructor injection and setter injection Differences in XML files. The only difference is that we have used the < constructor Arg > tag for constructor based injection and the < property > tag for setter based injection within the < bean > element.

The second important point to note is that if you pass a reference to an object, you need to use the ref attribute of the < property > tag. If you pass a value directly, you should use the value attribute.

After creating the source code and bean configuration file, let's run the application. If all goes well, this will print the following information:

XML configuration with P namespace: if you have many setter methods, you can easily use the XML configuration file with P namespace. Let's look at their differences:

Let's use the < property > tag as an example of a standard XML configuration file:

The above XML configuration can be rewritten using the following concise method of p-namespace:

Here, you should not specify the difference between the original value and the reference to the spatial object- The ref section indicates that this is not a straight chain value, but a reference to another bean.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>