Spring中的依赖注入(DI)方式

  最近学习了Spring中的依赖注入方式,做一下笔记。类A在代码中被其它对象都调用,之前做法是在被调用的地方通过new创建类A实例并通过set方法为类A对象属性赋值,这样就会导致维护比较麻烦。学习SpringIoc容器实例化对象很方便。接下来记录通过xml配置文件的方式的依赖注入的方式。
   准备工作: 先创建一个maven工程,建一个包包名自己随意起,编写一个UserService接口和UserServiceImpl接口实现类。
UserService:

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

pom文件:

<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,有参构造方法属性注入:
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配置:

<?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>

测试类和运行结果:

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();
    }
}

在这里插入图片描述

2,set方法属性注入(常用)
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配置:

<?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>

测试代码和上面相同,运行结果:
在这里插入图片描述
3、p名称空间属性注入(本质还是调用set方法,不常用)
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配置:

<?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>

运行结果:
在这里插入图片描述
4,SpEL属性注入(不常用):
SpEL:Spring Expression Language,Spring的表达式语言。
语法:#{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配置:

<?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>

运行结果:
在这里插入图片描述
加油吧少年

猜你喜欢

转载自blog.csdn.net/qq_42494654/article/details/111057534