Spring6梳理9—— 依赖注入之注入对象类型属性

9.1  依赖注入之外部注入对象类型属性

9.1.1  创建dept与emp类

1.dept类

package com.atguigu.spring6.iocxml.ditest;


//部门类
public class Dept {
    private String dname;

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public void info() {
        System.out.println("部门名称是:"+dname);
    }
}

 2.emp类

package com.atguigu.spring6.iocxml.ditest;
//员工类
public class Emp {
    private Dept dept;
    private String ename;
    private Integer age;

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public Integer getAge() {
        return age;
    }

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

    public void work() {
        System.out.println(ename + "工作到了" + age);
        dept.info();
    }
}

 由于一个部门有多个员工,因此部门与员工的关系为一对多,所以将唯一的Dept作为引用的bean,外键为

9.1.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">

    <bean id="dept" class="com.atguigu.spring6.iocxml.ditest.Dept">
        <property name="dname" value="安保部"></property>
    </bean>
    <bean id="emp" class="com.atguigu.spring6.iocxml.ditest.Emp">
        <property name="ename" value="lucy"></property>
        <property name="age" value="50"></property>
    <!--注入对象类型属性
       private Dept dept;
    -->
        <property name="dept" ref="dept"></property>
    </bean>
</beans>

9.1.3  创建测试类

package com.atguigu.spring6.iocxml.ditest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @package: com.atguigu.spring6.iocxml.ditest
 * @className: TestEmp
 * @Description:
 * @author: haozihua
 * @date: 2024/9/18 11:10
 */
public class TestEmp {
    public static void main(String[] args) {
        ApplicationContext context
                = new ClassPathXmlApplicationContext("bean-ref.xml");
        Emp emp = context.getBean("emp", Emp.class);
        emp.work();
    }
}

9.1.4  运行结果

由运行结果可以得出,当实体类出现需要注入对象的情况下,可以使用

<property name="dept" ref="dept"></property>

对对象进行引用,从而在实体类对象中注入另一个实体类对象,使用被注入的对象的方法变量等内容

9.2  依赖注入之内部注入对象类型

<!--    内部注入bean-->
    <bean id="emp2" class="com.atguigu.spring6.iocxml.ditest.Emp">
        <property name="ename" value="mary"></property>
        <property name="age" value="20"></property>
        <!--注入对象类型属性
           private Dept dept;
        -->
        <property name="dept"><!--与实体类保持一致-->
            <bean class="com.atguigu.spring6.iocxml.ditest.Dept">
                <property name="dname" value="财务部"></property>
            </bean>
        </property>
    </bean>

内部注入对象时,需要在<property></property>标签内部设置name,以及内部<bean></bean>标签中的class属性

由于获取Bean是基于getter()和setter()赋值,因此<property></property>标签的name属性值必须与实体类变量保持一致

9.3  依赖注入之内部注入对象类型

<!--    级联注入bean-->
    <bean id="emp3" class="com.atguigu.spring6.iocxml.ditest.Emp">
        <property name="ename" value="李湘"></property>
        <property name="age" value="30"></property>
        <property name="dept" ref="dept3"></property>
        <property name="dept.dname" value="人事部"></property>
    </bean>

级联注入对象时,需要注意property的name属性与实体类保持一致,ref必须引用的是xml文件中定义好的外部bean                                                                                              

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值