Spring's dependency injection DI

Dependency injection (DI) and inversion of control (IoC) are the same thing described from different perspectives, that is, by introducing IoC containers and using dependency injection to achieve decoupling between objects

Dependency injection mode:
1. Construction method injection
2. setter method injection
3. Interface injection
4. Complex type (set) injection

Code

//Computer class:
public class Computer {

    private String type;//Computer model
    private OperatingSystem operatingSystem;//operating system
        
    public Computer() {}
    public Computer(String type, OperatingSystem operatingSystem) {
        this.type = type;
        this.operatingSystem = operatingSystem;
    }
//Omit getter setter toString method here
}//OperatingSystem class (this is a property of Computer: operating system)

public class OperatingSystem {
    private String name;//Operating system name, such as Windows, Mac OS
  
  	//Operating system version, for example, when name is Windows and version is 10, it is win10
    private String version;

    public OperatingSystem(){}
    public OperatingSystem(String name, String version) {
        this.name = name;
        this.version = version;
    }
    //Omit getter setter toString method here

}

1. Construction method injection (make sure to write the corresponding construction method!)

This method uses the tags inside the bean tags for dependency injection.

Take the following construction method as an example

public Computer(String type, OperatingSystem operatingSystem) {
    this.type = type;
    this.operatingSystem = operatingSystem;
}

Write 1 (not recommended, not commonly used):

<bean id="operatingSystem" class="di.OperatingSystem">
    <constructor-arg index="0" value="macOS"/>
    <constructor-arg index="1" value="Mojave"/>
</bean>
<bean id="computer" class="di.Computer">
    <constructor-arg index="0" value="MacBook Pro 2019"/>
    <constructor-arg index="1" ref="operatingSystem"/>
</bean>

The writing method of the tag is the same as before. The only difference is that the tag body is used for attribute setting (dependency injection).

Each constructor Arg tag represents a parameter in the constructor;

The attribute index represents the order of parameters. Starting from 0, index = "0" represents the String type parameter of the construction method, and index = "1" represents the OperatingSystem parameter;

The attribute value represents the value to be passed in by this parameter. For example, the parameter type corresponds to Mac OS, and the version is Mojave. Generally speaking, the basic type and String type use the value attribute to pass in the value.

Because we refer to the operatingsystem object in the computer class, we need to define the operatingsystem bean for us to inject it into the bean. In fact, operatingsystem is a dependency of computer. In the second bean, there is a ref attribute, which is used to pass values that cannot be passed. In this case, the value of ref is the id of the bean operatingsystem.

Style 2 (recommended but not commonly used):

<bean id="operatingSystem" class="di.OperatingSystem">
    <constructor-arg name="name" value="macOS"/>
    <constructor-arg name="version" value="Mojave"/>
</bean>

<bean id="computer" class="di.Computer">
    <constructor-arg name="type" value="MacBook Pro 2019"/>
    <constructor-arg name="operatingSystem" ref="operatingSystem"/>
</bean>

The only difference between other places and write method 1 is that the index attribute in write method 1 is replaced by the name attribute, which is directly determined by the parameter name. It is clear and not easy to make mistakes.

2.setter method injection (make sure to write the empty parameter structure and all property setter methods!!! )

This approach uses properties inside the bean tag for dependency injection.

Take the following construction method as an example

public Computer(String type, OperatingSystem operatingSystem) {
    this.type = type;
    this.operatingSystem = operatingSystem;
}

Injection:

<bean id="operatingSystem" class="di.OperatingSystem">
    <property name="name" value="macOS"/>
    <property name="version" value="Mojave"/>
</bean>
<bean id="computer" class="di.Computer">
    <property name="type" value="MacBook Pro 2019"/>
    <property name="operatingSystem" ref="operatingSystem"/>
</bean>

In fact, it's similar to the second way of constructor injection, except that it's replaced by

3. Interface injection (useless, not to mention)

4. Collective injection
Note: the value tag in the following text should be replaced with the tag when the value to be set is a bean defined by itself.

Add keys to the Computer class to represent the collection of keys on the keyboard.

1. Array and List (interchangeable)
In fact, Spring equates Array with List.

Write method 1 (constructor Arg + array):

<bean id="computer" class="di.Computer">
    <constructor-arg name="keys">
        <array>
            <value>a</value>
            <value>b</value>
            <value>c</value>
        </array>
    </constructor-arg>
</bean>

Write method 2 (constructor Arg + list):

<bean id="computer" class="di.Computer">
    <constructor-arg name="keys">
            <list>
                <value>a</value>
                <value>b</value>
            </list>
    </constructor-arg>
</bean>

Write method 3 (property + array):

<bean id="computer" class="di.Computer">
    <property name="keys">
            <array>
                <value>a</value>
                <value>b</value>
            </array>
    </property>
</bean>

Write 4 (property + list):

<bean id="computer" class="di.Computer">
    <property name="keys">
            <list>
                <value>a</value>
                <value>b</value>
            </list>
    </property>
</bean>

2.Set
Write method 1 (constructor ARG):

<bean id="computer" class="di.Computer">
    <constructor-arg name="keys">
            <set>
                <value>a</value>
                <value>b</value>
            </set>
    </constructor-arg>
</bean>

property:

<bean id="computer" class="di.Computer">
    <property name="keys">
            <set>
                <value>a</value>
                <value>b</value>
            </set>
    </property>
</bean>

3.Map
Add the attribute map < string, string > softwares for the Computer class. The key indicates the software, and the value indicates the software installation directory

Add the attribute map < string, singer > songs for the Computer class. The key represents the song name and the value represents the singer object

property:

<bean id="singerEdSheeran" class="di.Singer">
    <property name="name" value="Ed Sheeran"/>
</bean>

<bean id="computer" class="di.Computer">
    <property name="softwares">
            <map>
                <entry key="WeChat" value="/Users/...."/>
                <entry key="Intellij Idea" value="/usr/local/...."/>
            </map>
    </property>
    <property name="songs">
        <map>
            <entry key="Shape of You" value-ref="singerEdSheeran"/>
            <entry key="I dont care" value-ref="singerEdSheeran"/>
        </map>
    </property>
</bean>

Write method 2 (change the property of write method 1 to constructor ARG)

Note that if the key or value is to refer to another bean, you need to replace it with or replace it with

13 original articles published, praised 0, 652 visitors
Private letter follow

Tags: Attribute Windows Mac Spring

Posted on Mon, 16 Mar 2020 03:30:02 -0700 by donturo