What is dependency injection (DI)

Dependency Injection (DI) is a very important topic in the Spring IOC container. Dependency injection (DI) is the process by which a Spring IOC container instantiates the parameters of the Bean constructor through a constructor, or sets the Bean’s member properties. This simply means passing arguments to the constructor with arguments or assigning values to some properties of the object when it is created.

Dependency injection (DDI) can be divided into dependency and injection. On what? Dependencies are the creation of classes (beans) that manage dependencies on the Spring IOC container. What about injection? Injection is used to inject required parameters into the parameterized constructor or to assign values to some member variables of the class.

2. Use dependency injection benefits

1) Use dependency injection to clean up your code and decouple your beans from other classes or properties without having to look up locations or classes.

2) Make it easier to test the class by getting the instantiated object of the class directly through the getBean () method.

3. There are two main ways to implement dependency injection

3.1 Dependency injection based on constructors

By default, the Spring IOC container uses the no-parameter constructor when creating class objects (beans). If the entity class uses the parameter constructor and is not configured in the Spring XML configuration file, the object cannot be initialized properly, and the following error will be reported:

1) Constructor arguments imported from other classes (beans)

The entity class User code looks like this:

public class User {
    private String name;
    private Integer age;
    // Define Address as a member variable
    private Address address;

    // The constructor introduces the Address class
    public User(Address address) {
        this.address = address; }}Copy the code

The content of the XML configuration file is as follows:

 <! -- Define Address class config -->
    <bean id="ar" class="com.hebeu.pojo.Address">
        <property name="address" value="Xi 'an"></property>
    </bean>

    <! -- Define User class config -->
    <bean id="user" class="com.hebeu.pojo.User">
        <! -- Specify the id of the imported class
        <constructor-arg ref="ar"></constructor-arg>
    </bean>
Copy the code

2) Constructor parameter types match

Matches by constructor parameter type, such as int; The String; Such as Boolean. The entity class User code looks like this:

// Member variables and parameter constructors in the User class
public class User {

    private String name;

    private Integer age;

    private String sex;

    public User(String name) {
        this.name = name;
        System.out.println("name:"+name); }}Copy the code

The content of the XML configuration file is as follows:

<! Constructor parameter type match, where name should be String, but not recommended, there may be arguments of the same type such as two strings -->

<constructor-arg type="java.lang.String" value="Shao han"></constructor-arg>
Copy the code

3) Constructor parameter index match

The User entity class has the parameter constructor name that can also specify the subscript of the name argument, starting at 0. The content of the XML configuration file is as follows:

<! Select * from index (); select * from index ();

<constructor-arg index="0" value="Li Yifeng"></constructor-arg>
Copy the code

4) Constructor parameter names match

The constructor passes in the parameter name, in this case a String name. The content of the XML configuration file is as follows:

<! -- constructor parameter name -->

<constructor-arg name="name" value="Chapter"></constructor-arg>
Copy the code

3.2 Dependency injection based on Setter methods in classes

Setter method injection means that the Spring IOC container instantiates the bean through the no-argument constructor and then calls the property set method in the entity class to assign values to member variables. Member variable types (base and reference types; Array type; The Set; The Map; List, etc.) there are eight main injection methods. Create a new Student entity of the above type as follows:

Student / / entities
public class Student {
    // Straight value (string, etc.)
    private String name;
    // Reference the object type
    private Address address;
    // Array type
    private String[] books;
    / / the List type
    private List<String> hobby;
    / / the Map class line
    private Map<String, String> card;
    / / Set type
    private Set<String> games;
    //Properties Configuration type
    private Properties info;
    // The string is set to null
    private String wife;
    }
Copy the code

The following XML configuration corresponds to the Student attribute one by one. The code for the attribute configuration is as follows:

1) The first is simple injection of direct values

 <! -- Direct value simple injection -->
 
<property name="name" value="Hair is not easy"></property>
Copy the code

2) The second, bean class reference injection

<! -- Bean class reference injection -->

 <property name="address" ref="address"></property>
Copy the code

3) The third way, array type property injection

 <! -- Array type property injection -->
        <property name="books">
            <array>
                <value>Quiet on the western front</value>
                <value>Happy together</value>
                <value>Steve jobs</value>
            </array>
        </property>
Copy the code

4) The fourth type, List collection type attribute injection

  <! Collection type attribute injection -->
        <property name="card">
            <map>
                <entry key="Identity card" value="1111313424"></entry>
                <entry key="Bank card" value="1111"></entry>
            </map>
        </property>
Copy the code

5) The fifth, Map set type attribute injection

<! -- Set Set type attribute injection -->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>CF</value>
                <value>wpw</value>
            </set>
        </property>      
Copy the code

6) The sixth kind, Set Set type attribute injection

 <! -- List set type attribute injection -->
        <property name="hobby">
            <list>
                <value>Listening to music</value>
                <value>coding</value>
                <value>Reading a book</value>
            </list>
        </property>        
Copy the code

7) Seventh, configure type property injection

 <! -- Properties -->
        <property name="info">
            <props>
                <prop key="url">http://localhost:9999/baidu.com</prop>
                <prop key="name">root</prop>
                <prop key="passwors">root</prop>
            </props>
        </property>
Copy the code

8) Empty string injection

  <! -- Empty string injection -->
        <property name="wife">
            <null/>
        </property>
Copy the code

4. To summarize

Learning Spring dependency injection knowledge points, write this blog to record important concepts and methods, convenient for later reading and learning. If there are mistakes or improvements, please point out, I will improve, refueling together!