Dependency Injection (DI)

  1. Dependency: The creation of Bean objects depends on containers, and the Bean objects depend on resources.
  2. Injection: Refers to the resource on which the Bean object depends, which is set and assembled by the container.

Injection pattern

  1. Constructor injection, see previous Blog IOC object creation and Spring configuration
  2. Set injection:To require a property to be injected, there must be a set method

For example, let’s create two entity classes

Student:

package pojo;

import java.util.*;

public class Student {
    private String name;                  // Basic type injection
    private Address address;             / / bean injection
    private String[] books;              // Array injection
    private List<String> hobbies;        / / the List injection
    private Map<String, String> card;    / / Map injection
    private Set<String> games;           / / Set injection
    private String wife;                 / / null values injection
    private Properties info;             / / Properties
    private String wifi;                 // null injection


    @Override
    public String toString(a) {
        return "Student:" + '\n' +
                "name='" + name + '\' ' + '\n' +
                "address=" + address + '\n' +
                "books=" + Arrays.toString(books) + '\n' +
                "hobbies=" + hobbies + '\n' +
                "card=" + card + '\n' +
                "games=" + games + '\n' +
                "wife='" + wife + '\' ' + '\n' +
                "info=" + info + '\n' +
                "wifi=" + wifi;
    }

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress(a) {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbies(a) {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public Map<String, String> getCard(a) {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames(a) {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife(a) {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo(a) {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    public String getWifi(a) {
        return wifi;
    }

    public void setWifi(String wifi) {
        this.wifi = wifi; }}Copy the code

Address:

package pojo;

public class Address {
    private String address;

    public String getAddress(a) {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString(a) {
        return "Address{" +
                "address='" + address + '\' ' +
                '} '; }}Copy the code

beans.xml


      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="pojo.Address">
        <property name="address" value="Fuzhou"/>
    </bean>

    <bean id="student" class="pojo.Student">

        <! -- Basic type injection -->
        <property name="name" value="Millet"/>

        <! -- Bean injection -->
        <property name="address" ref="address"/>

        <! -- Array injection -->
        <property name="books">
            <array>
                <value>java</value>
                <value>php</value>
                <value>python</value>
            </array>
        </property>

        <! -- List insert -->
        <property name="hobbies">
            <list>
                <value>code</value>
                <value>write</value>
                <value>music</value>
                <value>eat</value>
            </list>
        </property>

        <! -- Map injection uses Entry, different! -->
        <property name="card">
            <map>
                <entry key="Student id" value="123456"/>
                <entry key="Name" value="Millet"/>
            </map>
        </property>

        <! -- Set injection -->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>CSGO</value>
                <value>CF</value>
            </set>
        </property>

        <! -- null injection -->
        <property name="wife">
            <null/>
        </property>

        <! -- null injection -->
        <property name="wifi" value=""/>

        <! -- NULL indicates that the contents of the object are empty, i.e. the contents of the object are blank. -> No wife set. A null value indicates that the contents of the object cannot be determined -> Wifi is available, but it is not known what it is, and is not set. -->

        <! -- properties injection -->
        <property name="info">
            <props>
                <prop key="School">FAFU</prop>
                <prop key="College">Meter letter court</prop>
                <prop key="Professional">Software engineering</prop>
            </props>
        </property>


    </bean>

</beans>
Copy the code

The test class:

    @Test
    public void MyTest(a){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        Student student = (Student) context.getBean("student");

        System.out.println(student.toString());

    }
Copy the code

Test results:

Matters needing attention:

  1. Set injection and Properties injection are somewhat different from other injections. Note the differences.
  2. Difference between null and null:
    • Null indicates that the content of the object is empty, that is, the content of the object is blank. -> No wife set.
    • A null value indicates that the contents of the object cannot be determined -> Wifi is available, but it is not known what it is, and is not set.

P namespace injection and C namespace injection

  1. P namespace injection: equivalent to properties injection,Entity classes require no-parameter constructs

If you want to use p namespace, you need to introduce constraint file: p namespace XMLNS: p = “www.springframework.org/schema/p”

<bean id="teacher" class="pojo.Teacher" p:name="Flower" p:id="123456"/>
Copy the code
  1. C namespace injection: equivalent to constructor injection,Entity classes need parameter constructs

If you want to use namespace c, you need to introduce the constraints of the file: c namespace XMLNS: c = “www.springframework.org/schema/c”

<bean id="teacher1" class="pojo.Teacher" c:id="654321" c:name="Xiao Ming"/>
Copy the code