Dependency Injection (DI) in Spring

  Recently I learned about the dependency injection method in Spring and took some notes. Class A is called by other objects in the code. The previous practice was to create an instance of Class A through new in the called place and assign values ​​to the properties of Class A objects through the set method, which would lead to troublesome maintenance. It is very convenient to learn SpringIoc container to instantiate objects. Next, record the way of dependency injection through the xml configuration file.
   Preparation: First create a maven project, build a package and start with the package name at will, and write a UserService interface and UserServiceImpl interface implementation class.
UserService:

/*
* 保存用户接口
* */
public interface UserService {
    
    
    /*
    * 保存用户
    * */
    void saveUser();
}

pom file:

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
</dependencies>

1. Attribute injection with parameter construction method:
UserServiceImpl:

public class UserServiceImp implements UserService {
    
    
    private String name;
    private Integer age;
    private Date brithday;
    //通过有参构造方法为对象属性赋值
    public UserServiceImpl(String name, Integer age, Date brithday) {
    
    
        this.name = name;
        this.age = age;
        this.brithday = brithday;
    }
    public void saveUser() {
    
    
        System.out.println("saveUser方法执行了");
        System.out.println("姓名:" + name);
        System.out.println("年龄:" + age);
        System.out.println("生日:" + brithday);
    }
}

bean.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--构造方法注入
       constructor-arg标签:
         属性:
          index:指定参数在构造函数参数列表的索引位置
          type:指定参数在构造函数中的数据类型
          name:指定参数在构造函数中的名称用这个找给谁赋值
          value:它能赋的值是基本数据类型和String类型
          ref:它能赋的值是其他bean类型,也就是说必须得是在配置文件中配置过的bean
    -->
    <bean id="userService" class="com.zy.service.impl.UserServiceImpl">
        <constructor-arg name="name" value="小杨"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="brithday" ref="date"></constructor-arg>
    </bean>
    <bean id="date" class="java.util.Date"></bean>
</beans>

Test class and running result:

public class TestDemo01 {
    
    
    public static void main(String[] args) {
    
    
        //加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //根据bean标签的id获取实例对象
        UserService userService = (UserService) context.getBean("userService");
        //调用方法
        userService.saveUser();
    }
}

Insert picture description here

2. Set method attribute injection (commonly used)
UserServiceImpl is modified to the following code:

public class UserServiceImpl implements UserService {
    
    
    private String name;
    private Integer age;
    private Date brithday;
    public void setName(String name) {
    
    
        this.name = name;
    }
    public void setAge(Integer age) {
    
    
        this.age = age;
    }
    public void setBrithday(Date brithday) {
    
    
        this.brithday = brithday;
    }
    public void saveUser() {
    
    
        System.out.println("saveUser方法执行了");
        System.out.println("姓名:" + name);
        System.out.println("年龄:" + age);
        System.out.println("生日:" + brithday);
    }
}

bean.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
    set方法注入:常用
      标签:property
         属性:
         name:找的是类中set方法后面的部分
         ref:给属性赋值是其他bean类型的
         value:给属性赋值是基本数据类型和string类型的
    -->
    <bean id="userService" class="com.zy.service.impl.UserServiceImpl">
        <property name="name" value="小张"/>
        <property name="age" value="20"/>
        <property name="brithday" ref="date"/>
    </bean>
    <bean id="date" class="java.util.Date"></bean>
</beans>

The test code is the same as the above, the running result:
Insert picture description here
3. p namespace attribute injection (the essence is to call the set method, not commonly used)
UserServiceImpl:

public class UserServiceImpl implements UserService {
    
    
    private String name;
    private Integer age;
    private Date brithday;
    public void setName(String name) {
    
    
        this.name = name;
    }
    public void setAge(Integer age) {
    
    
        this.age = age;
    }
    public void setBrithday(Date brithday) {
    
    
        this.brithday = brithday;
    }
    public void saveUser() {
    
    
        System.out.println("saveUser方法执行了");
        System.out.println("姓名:" + name);
        System.out.println("年龄:" + age);
        System.out.println("生日:" + brithday);
    }
}

bean.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
       p名称空间注入:不常用
    -->
    <bean id="userService" class="com.zy.service.impl.UserServiceImpl"
       p:name="小李" p:age="20" p:brithday-ref="date">
    </bean>
    <bean id="date" class="java.util.Date"></bean>
</beans>

Running results:
Insert picture description here
4. SpEL attribute injection (not commonly used):
SpEL: Spring Expression Language, Spring's expression language.
Syntax: #{SpEL}

public class UserServiceImpl implements UserService {
    
    
    private String name;
    private Integer age;
    private Date brithday;
    public void setName(String name) {
    
    
        this.name = name;
    }
    public void setAge(Integer age) {
    
    
        this.age = age;
    }
    public void setBrithday(Date brithday) {
    
    
        this.brithday = brithday;
    }
    public void saveUser() {
    
    
        System.out.println("saveUser方法执行了");
        System.out.println("姓名:" + name);
        System.out.println("年龄:" + age);
        System.out.println("生日:" + brithday);
    }
}

bean.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
    SpEL注入:不常用
    -->
    <bean id="userService" class="com.zy.service.impl.UserServiceImpl">
        <property name="name" value="#{'小明'}"></property>
        <property name="age" value="#{23}"></property>
        <property name="brithday" ref="#{'date'}"></property>
    </bean>
    <bean id="date" class="java.util.Date"></bean>
</beans>

Operation result:
Insert picture description here
Come on, boy

Guess you like

Origin blog.csdn.net/qq_42494654/article/details/111057534