Spring的依赖注入

53 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第20天,点击查看活动详情

依赖注入之setter

创建学生类Student
package com.atguigu.spring.pojo;/**
 * @author zzw
 * @version 1.0
 * Create by 2022/10/15 17:34
 *//**
 * @author zzw
 * @version 1.0
 * Create by 2022/10/15 17:34
 */
public class Student implements Person {
    private Integer sid;
    private String sname;
    private Integer age;
    private String gender;
​
    public Student() {
    }
​
    public Student(Integer sid, String sname, Integer age, String gender) {
        this.sid = sid;
        this.sname = sname;
        this.age = age;
        this.gender = gender;
    }
​
    public Integer getSid() {
        return sid;
    }
​
    public void setSid(Integer sid) {
        this.sid = sid;
    }
​
    public String getSname() {
        return sname;
    }
​
    public void setSname(String sname) {
        this.sname = sname;
    }
​
    public Integer getAge() {
        return age;
    }
​
    public void setAge(Integer age) {
        this.age = age;
    }
​
    public String getGender() {
        return gender;
    }
​
    public void setGender(String gender) {
        this.gender = gender;
    }
​
    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + ''' +
                ", age=" + age +
                ", gender='" + gender + ''' +
                '}';
    }
}
配置bean时为实体类中的属性赋值
 <bean id="studentTwo" class="com.atguigu.spring.pojo.Student">
<!--        property :通过成员变量的set方法进行赋值
            name:设置需要赋值的属性名(和set方法有关)
            value:设置为属性所赋的值
            -->
        <property name="sid" value="100"></property>
        <property name="sname" value="张1"></property>
        <property name="age" value="22"></property>
        <property name="gender" value="女"></property>
    </bean>
测试
@Test
public void testDI(){
    ApplicationContext ioc=new ClassPathXmlApplicationContext("spring-ioc.xml");
    Student studentTwo = ioc.getBean("studentTwo", Student.class);
    System.out.println("studentTwo = " + studentTwo);
}

依赖注入之构造器注入

配置bean
<bean id="studentThree" class="com.atguigu.spring.pojo.Student">
<!--    有参构造    name 为参数名-->
<!--       如果有参构造为这种情况
public Student(Integer sid, String sname, String gender , Integer age) {
        this.sid = sid;
        this.sname = sname;
        this.age = age;
        this.gender = gender;
    }
​
    public Student(Integer sid, String sname, String gender, Double score) {
        this.sid = sid;
        this.sname = sname;
        this.gender = gender;
        this.score = score;
    } 需要在constructor-arg中写上name标识好对应属性-->
        <constructor-arg value="200"></constructor-arg>
        <constructor-arg value="王五"></constructor-arg>
        <constructor-arg value="男"></constructor-arg>
        <constructor-arg value="25" name="age"></constructor-arg>
    </bean>
测试
@Test
public void testDI1(){
    ApplicationContext ioc=new ClassPathXmlApplicationContext("spring-ioc.xml");
    Student studentThree = ioc.getBean("studentThree", Student.class);
    System.out.println("studentThree = " + studentThree);
}

image-20221015205302958.png

依赖注入之特殊值处理

配置bean
   <bean id="studentFour" class="com.atguigu.spring.pojo.Student">
        <!--        property :通过成员变量的set方法进行赋值
                    name:设置需要赋值的属性名(和set方法有关)
                    value:设置为属性所赋的值
                    -->
<!--
<:&lt;
>:&gt;
CDATA节其中的内容会原样解析<![CDATA[...]]>
CDATA节是xml中一个特殊的标签 因此不能写在一个属性中-->
        <property name="sid" value="300"></property>
        <property name="sname">
            <value><![CDATA[<王五>]]></value>
        </property><!--    如果直接使用value赋值null得到的是null这个字符串
<property name="gender" value="null"></property>-->
<!--        改用下面的方法-->
        <property name="gender">
        <null/> </property>
​
    </bean>
测试类
@Test
public void testDI2(){
    ApplicationContext ioc=new ClassPathXmlApplicationContext("spring-ioc.xml");
    Student studentFour = ioc.getBean("studentFour", Student.class);
    System.out.println("studentFour = " + studentFour);
}
测试结果

image-20221015214929230.png

依赖注入之为类型属性赋值

引用外部的bean
<bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    <!--        property :通过成员变量的set方法进行赋值
                name:设置需要赋值的属性名(和set方法有关)
                value:设置为属性所赋的值
                -->
    <property name="sid" value="400"></property>
    <property name="sname" value="蜘蛛网"></property>
    <property name="age" value="22"></property>
    <property name="gender" value="男"></property>
    <property name="clazz" ref="clazzOne"></property>
</bean>
<bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
    <property name="cid" value="1111"></property>
    <property name="cName" value="最强王者版"></property>
</bean>
测试类
@Test
public void testDI3(){
    ApplicationContext ioc=new ClassPathXmlApplicationContext("spring-ioc.xml");
    Student studentFive = ioc.getBean("studentFive", Student.class);
    System.out.println("studentFive = " + studentFive);
}
测试结果

image-20221015222006586.png

级联方式和内部bean
级联方式
<bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    <!--        property :通过成员变量的set方法进行赋值
                name:设置需要赋值的属性名(和set方法有关)
                value:设置为属性所赋的值
                -->
    <property name="sid" value="400"></property>
    <property name="sname" value="蜘蛛网"></property>
    <property name="age" value="22"></property>
    <property name="gender" value="男"></property>
    <property name="clazz" ref="clazzOne"></property>
    <property name="clazz.cid" value="222"></property>
    <property name="clazz.cName" value="远大启程"></property>
</bean>

一般不用级联方式

内部bean
<bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    <!--        property :通过成员变量的set方法进行赋值
                name:设置需要赋值的属性名(和set方法有关)
                value:设置为属性所赋的值
                -->
    <property name="sid" value="400"></property>
    <property name="sname" value="蜘蛛网"></property>
    <property name="age" value="22"></property>
    <property name="gender" value="男"></property>
    <property name="clazz">
        <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
            <property name="cid" value="123"></property>
            <property name="cName" value="盛大的"></property>
        </bean>
    </property>
</bean>

image-20221015223735062.png

依赖注入之为数组类型的属性赋值
  <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
        <!--        property :通过成员变量的set方法进行赋值
                    name:设置需要赋值的属性名(和set方法有关)
                    value:设置为属性所赋的值
                    -->
        <property name="sid" value="400"></property>
        <property name="sname" value="蜘蛛网"></property>
        <property name="age" value="22"></property>
        <property name="gender" value="男"></property>
        <property name="clazz">
            <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
                <property name="cid" value="123"></property>
                <property name="cName" value="盛大的"></property>
            </bean>
        </property>
        <property name="hobby">
            <array>
<!--                如果是实体类中的字面量类型中那就是用value
如果是其他实体类的类型就是用ref-->
                <value>抽烟</value>
                <value>喝酒</value>
                <value>烫头</value>
            </array>
        </property>

image-20221015224844970.png

<bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
    <property name="cid" value="1111"></property>
    <property name="cName" value="最强王者版"></property>
    <property name="student">
        <array>
            <ref bean="studentFive" ></ref>
        </array>
    </property>
</bean>

image-20221015224918792.png

依赖注入之为list集合类型的属性赋值
<bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
        <property name="cid" value="1111"></property>
        <property name="cName" value="最强王者版"></property>
        <property name="student">
            <array>
                <ref bean="studentFive" ></ref>
            </array>
        </property>
        <property name="students" ref="studentList">
<!--            <list>-->
<!--                <ref bean="studentOne"></ref>-->
<!--                <ref bean="studentTwo"></ref>-->
<!--                <ref bean="studentThree"></ref>-->
<!--            </list>-->
        </property>
    </bean>
<!--    配置一个集合类型的bean,需要使用util的约束-->
    <util:list id="studentList">
        <ref bean="studentOne"></ref>
        <ref bean="studentTwo"></ref>
        <ref bean="studentThree"></ref>
    </util:list>

image-20221015230708952.png 依赖注入之为map集合类型的属性赋值

 <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
        <!--        property :通过成员变量的set方法进行赋值
                    name:设置需要赋值的属性名(和set方法有关)
                    value:设置为属性所赋的值
                    -->
        <property name="sid" value="400"></property>
        <property name="sname" value="蜘蛛网"></property>
        <property name="age" value="22"></property>
        <property name="gender" value="男"></property>
        <property name="clazz">
            <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
                <property name="cid" value="123"></property>
                <property name="cName" value="盛大的"></property>
            </bean>
        </property>
        <property name="hobby">
            <array>
<!--                如果是实体类中的自变量那就是用value
如果是其他实体类的类型就是用ref-->
                <value>抽烟</value>
                <value>喝酒</value>
                <value>烫头</value>
            </array>
        </property>
        <property name="teacherMap" ref="teacherMap">
<!--            <map>-->
<!--                <entry key="10086" value-ref="teacherOne"></entry>-->
<!--                <entry key="10010" value-ref="teacherTwo"></entry>-->
<!--&lt;!&ndash;字面量类型用value和key-->
<!--类类型用value-ref key-ref&ndash;&gt;-->
<!--            </map>-->
        </property>
    </bean>
    <util:map id="teacherMap">
        <entry key="10086" value-ref="teacherOne"></entry>
        <entry key="10010" value-ref="teacherTwo"></entry>
    </util:map>

image-20221015232017422.png

依赖注入之p命名空间
<bean id="studentSix" class="com.atguigu.spring.pojo.Student"
p:sid="1005" p:sname="小明" p:teacherMap-ref="teacherMap">
</bean>

image-20221015232530241.png