Java第三十八天,Spring框架系列,依赖注入——配置文件注入方式 一、什么是依赖注入 二、能注入的数据类型 三、注入的方式 四、构造函数注入方式 五、set方法注入方式 六、复合/复杂/集合类型注入方式 七、项目架构

依赖关系的管理,都交由spring框架管理和维护,当前类需要用到的其他类资源,由spring为我们提供,我们只需要在配置文件中声明。而依赖关系的维护,就叫做依赖注入

二、能注入的数据类型

  • 基本类型和String
  • 真他bean类型(在配置文件中或者注解配置过的bean)
  • 复杂类型/集合类型

三、注入的方式

  • 使用构造函数提供
  • 使用set方法提供
  • 使用注解提供

注意:三种注入方式在被注入具体的值得时候,双引号(字符串标识)写不写都无所谓,因为spring始终把注入的初始值视为字符串类型

四、构造函数注入方式

构造方法注入方式要求类的每个成员属性都必须有值

1.教程

使用<bean></bean>标签的<constructor-arg></constructor-arg>内部标签声明

<bean >
        <constructor-arg [option]="" value=""></constructor-arg>   
        <constructor-arg [option]="" ref="[自定义BeanID]"></constructor-arg>
</bean>
<bean ></bean>


1.其中option中的可选参数如下:
type ===> 用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型(不适用与包含多个同类型参数的构造函数)
index ===> 用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置是从0开始(不常用)
name ===> 用于指定给构造函数中指定名称的参数赋值(常用)

2.
value代表给第1步指定的参数赋值;
赋值的时候,所有的值都被看作是字符串,有的类型可以被spring智能转换,有的不可以,例如Date类型。这就需要用到ref了
3.ref用来配合constructor-arg标签来完成对 spring不能智能转换参数类型 的转化

2.举例

被注入数据的类

package com.huhai.Dao.Impl;

import com.huhai.Dao.IAccountDao;

import java.util.Date;

/**
 * 持久层实现类
 */
public class AccountDaoImpl implements IAccountDao {

    private String name;
    private Integer activeDays;
    private Date activeDate;

    public AccountDaoImpl(String name, Integer activeDays, Date activeDate) {
        this.name = name;
        this.activeDays = activeDays;
        this.activeDate = activeDate;
    }

    public void save() {
        System.out.println(name + " " + activeDate + " " + activeDays);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!--把对象的创建交给spring来管理-->
    <bean ></bean>

    <bean >
        <constructor-arg name="name" value="lanyue"></constructor-arg>
        <constructor-arg name="activeDays" value="100"></constructor-arg>
        <constructor-arg name="activeDate" ref="myDate"></constructor-arg>
    </bean>
    <!--对应上面的ref参数指向的myDate-->
    <bean ></bean>

</beans>

五、set方法注入方式

set方法无法保证被注入类的每个成员属性都一定有值

1.教程

①首先在被注入的类中生成set方法

②利用<bean></bean>标签的内部标签<property></property>为类属性注入数据

参数:
1.name ===> 用于指定给构造函数中指定名称的参数赋值(常用)
2.value代表给第1步指定的参数赋值
赋值的时候,所有的值都被看作是字符串,有的类型可以被spring智能转换,有的不可以,例如Date类型。这就需要用到ref了
3.ref用来配合constructor-arg标签来完成对 spring不能智能转换参数类型 的转化

<bean >
        <!--name参数的值只与set方法有关,而与set方法要设置的属性名称无关-->
        <property name="" value=""></property>
        <!--如果set参数无法满足要求时,需要用到ref参数-->
        <property name="" ref="[引用ID]"></property>
</bean>

<bean ></bean>

2.举例

被注入的类

package com.huhai.Dao.Impl;

import com.huhai.Dao.IAccountDao;

import java.util.Date;

/**
 * 持久层实现类
 */
public class AccountDaoImpl implements IAccountDao {

    private String name;
    private Integer activeDays;
    private Date activeDate;

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

    public void setActiveDays(Integer activeDays) {
        this.activeDays = activeDays;
    }

    public void setActiveDate(Date activeDate) {
        this.activeDate = activeDate;
    }

    public void save() {
        System.out.println(name + " " + activeDate + " " + activeDays);
    }
}

注入类数据的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!--把对象的创建交给spring来管理-->
    <bean ></bean>

    <!--set方法注入方式-->
    <bean >
        <property name="name" value="有限公司"></property>
        <property name="activeDate" ref="myDate"></property>
        <property name="activeDays" value="200"></property>
    </bean>
    <bean ></bean>


</beans>

六、复合/复杂/集合类型注入方式

此方法必须选取set方法或构造方法中的一种作为基础注入方式,然后利用<property/>或<constructor-arg>内的子标签<array/><list/><map/>等等注入

1.步骤

①生成带参构造方法或者set方法

②以构造方法注入方式或者set方法注入方式为基础,在结合二者的子标签进行注入

2.举例

package com.huhai.Dao.Impl;

import com.huhai.Dao.IAccountDao;

import java.lang.reflect.Array;
import java.util.*;

/**
 * 持久层实现类
 */
public class AccountDaoImpl implements IAccountDao {

    private String[] myStr;
    private List<String> myList;
    private Set<String> mySet;
    private Map<String, String> myMap;
    private Properties myPro;

    public void setMyStr(String[] myStr) {
        this.myStr = myStr;
    }

    public void setMyList(List<String> myList) {
        this.myList = myList;
    }

    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }

    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }

    public void setMyPro(Properties myPro) {
        this.myPro = myPro;
    }

    public void save() {
        System.out.println(Arrays.toString(myStr));
        System.out.println(myList);
        System.out.println(mySet);
        System.out.println(myMap);
        System.out.println(myPro);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!--把对象的创建交给spring来管理-->
    <bean ></bean>

    <!--set方法注入方式-->
    <bean >
        <property name="myStr">
            <array>
                <value>"第一个"</value>
                <value>"第二个"</value>
                <value>"第三个"></value>
            </array>
        </property>

        <property name="myList">
            <list>
                <value>"第一个"</value>
                <value>"第二个"</value>
                <value>"第三个"></value>
            </list>
        </property>

        <property name="mySet">
            <set>
                <value>"第一个"</value>
                <value>"第二个"</value>
                <value>"第三个"></value>
            </set>
        </property>

        <property name="myMap">
            <map>
                <!--第一种写法-->
                <entry key="第一个key" value="第一个value"></entry>
                <!--第二种写法-->
                <entry key="第二个key">
                    <value>"第二个value"</value>
                </entry>
                <entry key="第三个key" value="第三个value"></entry>
            </map>
        </property>
        
        <property name="myPro">
            <props>
                <prop key="第一个Key">"第一个value"</prop>
                <prop key="第二个Key">"第二个value"</prop>
                <prop key="第三个Key">"第三个value"</prop>
            </props>
        </property>

    </bean>
    <bean ></bean>


</beans>

七、项目架构

Java第三十八天,Spring框架系列,依赖注入——配置文件注入方式
一、什么是依赖注入
二、能注入的数据类型
三、注入的方式
四、构造函数注入方式
五、set方法注入方式
六、复合/复杂/集合类型注入方式
七、项目架构

1.持久层接口

package com.huhai.Dao;

/**
 *持久层接口
 */
public interface IAccountDao {
    public abstract void save();
}

2.持久层接口实现类(不同的注入方式对应的代码可能会不同)

package com.huhai.Dao.Impl;

import com.huhai.Dao.IAccountDao;

import java.lang.reflect.Array;
import java.util.*;

/**
 * 持久层实现类
 */
public class AccountDaoImpl implements IAccountDao {

    private String[] myStr;
    private List<String> myList;
    private Set<String> mySet;
    private Map<String, String> myMap;
    private Properties myPro;

    public void setMyStr(String[] myStr) {
        this.myStr = myStr;
    }

    public void setMyList(List<String> myList) {
        this.myList = myList;
    }

    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }

    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }

    public void setMyPro(Properties myPro) {
        this.myPro = myPro;
    }

    public void save() {
        System.out.println(Arrays.toString(myStr));
        System.out.println(myList);
        System.out.println(mySet);
        System.out.println(myMap);
        System.out.println(myPro);
    }
}

3.业务层接口

package com.huhai.Service;

/**
 *业务层接口
 */
public interface IAccountService {
    public abstract void save();
}

4.业务层接口实现类

package com.huhai.Service.Impl;

import com.huhai.Dao.IAccountDao;
import com.huhai.Service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;

/**
 * 业务层实现类
 */
public class AccountServiceImpl implements IAccountService {

    public void save() {

        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountDao as = ac.getBean("accountDaoImpl", IAccountDao.class);
        as.save();
    }
}

5.表现层

package com.huhai;

import com.huhai.Service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Realize {

    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = ac.getBean("accountServiceImpl", IAccountService.class);
        as.save();
    }
}

6.注入配置文件(bean.xml,文件名可随意)(每种注入方式的配置文件已经在示例中给出)

7.项目配置文件pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>demo13</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
    </dependencies>

</project>