文章目录

  • Spring DI
  • Spring set方法注入
  • a.普通方法注入
  • b.自定义bean注入
  • c.自动装配
  • Spring基于构造方法的注入

接上一篇Spring框架的IOC后,我们来聊一聊Spring框架的依赖注入(DI)。

Spring DI

  在Spring创建对象的过程中Spring可以依据配置对对象的属性进行设置,这个过程称之为依赖注入,即DI。由于我们把创建对象和把对象的生命周期的管理都交给了Spring框架管理,所以我们索性不再java代码中设置属性的值而是把设置属性值这个工作交给xml配置文件以便更加简洁java代码。

Spring set方法注入

  通常javabean属性都会私有化,而对外暴露setXxx()和getXxx()方法,此时Spring可以通过set方法将属性的值注入给对象

a.普通方法注入

1.创建一个Person类的javabean,其中设置基本数据类型、String类型、集合类型、映射类型、Properties类型。

public class Person {
    private String name;
    private int age;
    private List<String> list;
    private Set<String> set;
    private Map<String,String> map;
    private Properties pros;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Properties getPros() {
        return pros;
    }

    public void setPros(Properties pros) {
        this.pros = pros;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                ", pros=" + pros +
                '}';
    }
}

2.在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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="person" class="cn.springDI.domain.Person">
    
        <!--基本类型(基本数据类型、String类型)设置-->
        <property name="name" value="张三"></property>
        <property name="age" value="19"></property>

        <!--集合类型-->
        <property name="list">
            <list>
                <value>v1</value>
                <value>v2</value>
                <value>v3</value>
            </list>
        </property>

        <property name="set">
            <set>
                <value>v1</value>
                <value>v2</value>
                <value>v3</value>
            </set>
        </property>

        <!--映射类型-->
        <property name="map">
            <map>
                <entry key="k1" value="v1"></entry>
                <entry key="k2" value="v2"></entry>
                <entry key="k3" value="v3"></entry>
            </map>
        </property>

        <!--Properties类型-->
        <property name="pros">
            <props>
                <prop key="k1">v1</prop>
                <prop key="k2">v2</prop>
                <prop key="k3">v3</prop>
            </props>
        </property>
    </bean>
</beans>

3.创建一个test类测试属性是否注入成功

public class Test01 {
    @Test
    public void test01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Person person= (Person) context.getBean("person");
        System.out.println(person.getName());
        System.out.println(person.getAge());
        System.out.println(person.getList());
        System.out.println(person.getMap());
        System.out.println(person.getPros());
    }
}

4.查看打印结果

Junit 中 spring注入失败 springdi注入_xml

b.自定义bean注入

当我们所创建的javabean有自定义属性的时候我们可以使用这个方法
1.创建一个Person类

public class Person {
    private String name;
    private int age;
    private Dog dog;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", dog=" + dog +
                '}';
    }
}

2.创建一个Dog类

public class Dog {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3.在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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--Dog类的bean标签-->
    <bean id="dog" class="cn.springDI.domain.Dog">
        <property name="name" value="旺财"></property>
        <property name="age" value="5"></property>
    </bean>
    <!--Person类的bean标签-->
    <bean id="person" class="cn.springDI.domain.Person">
        <property name="name" value="张三"></property>
        <property name="age" value="18"></property>
        <!--注入Dog属性-->
        <property name="dog" ref="dog"></property>
    </bean>

</beans>

4.看输出结果

Junit 中 spring注入失败 springdi注入_Junit 中 spring注入失败_02

c.自动装配

  在Spring的set方法实现的注入过程中,支持自动装配机制,所谓自动装配机制,会根据要这设置的javabean属性的名字或类型到Spring中自动寻早对应的id或者类型的<bean>进行设置,从而省去依次配置的过程,简化了配置
1.为指定<bean>开启自动配置

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

    <!--Dog类的bean标签-->
    <bean id="dog" class="cn.springDI.domain.Dog">
        <property name="name" value="旺财"></property>
        <property name="age" value="5"></property>
    </bean>
    
    <!--Person类的bean标签-->
    <bean id="person" class="cn.springDI.domain.Person" autowire="byName">
        <property name="name" value="张三"></property>
        <property name="age" value="18"></property>
    </bean>

</beans>

2.为全局配置自动装配

<?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"
       default-autowire="byName"
>

<!--Dog类的bean标签-->
<bean id="dog" class="cn.springDI.domain.Dog">
    <property name="name" value="旺财"></property>
    <property name="age" value="5"></property>
</bean>

<!--Person类的bean标签-->
<bean id="person" class="cn.springDI.domain.Person">
    <property name="name" value="张三"></property>
    <property name="age" value="18"></property>
</bean>
    
</beans>

Spring基于构造方法的注入

  对象属性设置的另一种方式在对象创建的过程中通过构造方法传入并设置对象属性。Spring也可以通过这样的构造方法实现属性的注入。
1.创建javabean

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

2.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--
             index:为构造方法的第几个参数 进行配置
             name:为构造方法的哪个名字的参数进行配置
             **index 和 name 可以配置任何一个或同时配置 但要求一旦配置必须正确
             **推荐优先使用index方式配置 防止没有源码造成name无法匹配到对应参数
             type:该构造方法参数的类型
             value:该构造方法参数的值 ,用来指定基本值
             ref:该构造方法参数的值,用来指定引用其他bean的值
                         -->
    <bean id="person" class="cn.springDI.domain.Person">
        <constructor-arg index="0" value="张三"/>
        <constructor-arg index="1" value="18"/>
    </bean>
</beans>