SoFunction
Updated on 2025-03-08

Detailed explanation of Spring's DI dependency injection

1. What is DI dependency injection?

spring dynamically provides an object with other objects it needs. This is achieved through DI (Dependency Injection). For example, object A needs to operate the database. In the past, we always wrote code in A to obtain a Connection object. With spring, we only need to tell spring that a Connection is needed in A. As for how to construct this Connection and when to construct it, A does not need to know. When the system is running, spring will create a Connection at the appropriate time, and then inject it into A like an injection, thus completing the control of the relationship between each object. A needs to depend on Connection to run normally, and this Connection is injected into A by spring, and the name of dependency injection comes from this. So how is DI implemented? An important feature after Java 1.3 is reflection, which allows the program to dynamically generate objects, execute objects methods, and change objects' properties when running. Spring is injected through reflection.

Simply put, what is dependency injection is to assign values ​​to attributes (including basic data types and reference data types)

2. Use the set method to assign values ​​to attributes

Step 1: Create a project and import the corresponding jar package

Step 2: Create entity class Person

package ;
 
import ;
import ;
import ;
import ;
 
public class Person {
    private Long pid;
    private String pname;
    private Student students;
    private List lists;
    private Set sets;
    private Map maps;
    private Properties properties;
     
    public Long getPid() {
        return pid;
    }
    public void setPid(Long pid) {
         = pid;
    }
    public String getPname() {
        return pname;
    }
    public void setPname(String pname) {
         = pname;
    }
    public Student getStudents() {
        return students;
    }
    public void setStudents(Student students) {
         = students;
    }
    public List getLists() {
        return lists;
    }
    public void setLists(List lists) {
         = lists;
    }
    public Set getSets() {
        return sets;
    }
    public void setSets(Set sets) {
         = sets;
    }
    public Map getMaps() {
        return maps;
    }
    public void setMaps(Map maps) {
         = maps;
    }
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
         = properties;
    }
     
}

We see that this entity class includes the reference type Student class, the basic data class, and the collection data type.

Step 3: Assign value in

<!--
    propertyIt is used to describe the properties of a class
    Basic types of encapsulation classes、StringFor the type of value requiredvalueAssignment
    For reference typesrefAssignment
-->
<bean  class="">
    <property name="pid" value="1"></property>
    <property name="pname" value="vae"></property>
    <property name="students">
        <ref bean="student"/>
    </property>
     
    <property name="lists">
        <list>
            <value>1</value>
            <ref bean="student"/>
            <value>vae</value>
        </list>
    </property>
     
    <property name="sets">
        <set>
            <value>1</value>
            <ref bean="student"/>
            <value>vae</value>
        </set>
    </property>
     
    <property name="maps">
        <map>
            <entry key="m1" value="1"></entry>
            <entry key="m2" >
                <ref bean="student"/>
            </entry>
        </map>
    </property>   
     
    <property name="properties">
        <props>
            <prop key="p1">p1</prop>
            <prop key="p2">p2</prop>
        </props>
    </property>  
     
</bean>
 
 
<bean  class=""></bean>

Step 4: Test

//Use the set method to assign values ​​to the object    @Test
    public void testSet(){
        //1. Start the spring container        //2. Take out data from spring container        //3. Calling methods through objects        ApplicationContext context =
                new ClassPathXmlApplicationContext("");
        Person person = (Person) ("person");
        (());//vae
    }

3. Use the constructor to assign values ​​to attributes

Step 1: Add two constructors in entity class Per': with parameters and without parameters

//Default constructor    public Person(){}
    //Constructor with parameter    public Person(Long pid,Student students){
         = pid;
         = students;
    }

Step 2: Assign value in

<!-- Assign value according to constructor -->
    <!--
        index  Represents the position of the parameter  from0Start the calculation
        type   Refers to the type of parameter,When there are multiple constructors,Can be usedtypeLet's distinguish,If you can determine which constructor it is,You don't need to writetype
        value  Assign values ​​to basic types
        ref    Assign values ​​to reference types
      -->
    <bean  class="">
        <constructor-arg index="0" type="" value="1">
        </constructor-arg>       
        <constructor-arg index="1" type="" ref="student_con"></constructor-arg>
    </bean>
    <bean  class=""></bean>

Step 3: Test

//Use the constructor to assign values ​​to the object    @Test
    public void testConstrutor(){
        ApplicationContext context = new ClassPathXmlApplicationContext("");
        Person person = (Person) ("person_con");
        (());//1
    }

Summarize:

1. If the element <constructor-arg> is not present in the bean in the spring configuration file, the default constructor is called.

2. If there is <constructor-arg> in the bean in the spring configuration file, the element determines the unique constructor.

That’s all for this article. I hope it can help you and I hope you can pay more attention to more of my content!