【Java】【Spring】xml管理Bean-依赖注入-对象类型属性-引用外部bean、引用内部bean、级联注入

有两个类,部门和员工。部门和员工的对应关系是一对多(1:m)。
在这里插入图片描述
部门类Dept:

package com.kiong.spring6;

public class Dept {
    private String deptName;

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
    public void printInfo(){
        System.out.println("部门名称是:"+deptName);
    }
}

员工类Employee类:

package com.kiong.spring6;

public class Employee {
    private String name;
    private int age;
    Dept dpt;

    public Employee() {
    }

    public Employee(String name, int age, Dept dpt) {
        this.name = name;
        this.age = age;
        this.dpt = dpt;
    }

    public void work(){
        System.out.println("员工"+name+"的年龄是"+age);
        dpt.printInfo();
    }

    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 Dept getDpt() {
        return dpt;
    }

    public void setDpt(Dept dpt) {
        this.dpt = dpt;
    }

}

方式1:引用外部bean。xml文件beanEmp.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">

<!--方式1,引用外部bean
    1 创建两个对象,dept和emp
    2 在emp的bean标签里面,使用property引入dept的bean
-->

    <bean id="dept" class="com.kiong.spring6.Dept">
        <property name="deptName" value="安保部"></property>
    </bean>
    
    <bean id="emp" class="com.kiong.spring6.Employee">
        <!--普通属性注入-->
        <property name="name" value="Kiong"></property>
        <property name="age" value="21"></property>
        <!--注入对象类型属性
            private Dept dpt;
        -->
        <property name="dpt" ref="dept"></property>
    </bean>
</beans>

测试类TestEmp:

package com.kiong.spring6;

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

public class TestEmp {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("beanEmp.xml");
        Employee emp = context.getBean("emp",Employee.class);
        emp.work();

    }
}

方式1运行结果如下:
在这里插入图片描述
方式2:引用内部bean。xml文件beanEmp.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">

<!--方式2,内部bean
-->
    
    <bean id="emp" class="com.kiong.spring6.Employee">
        <!--普通属性注入-->
        <property name="name" value="Lisa"></property>
        <property name="age" value="20"></property>

        <!--内部bean注入-->
        <property name="dpt">
            <bean id="dept" class="com.kiong.spring6.Dept">
                <property name="deptName" value="人力部"></property>
            </bean>
        </property>
    </bean>
</beans>

方式2运行结果如下:
在这里插入图片描述
方式3:级联注入。xml文件beanEmp.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">

<!--方式3,级联属性注入
-->
    <bean id="dept" class="com.kiong.spring6.Dept">
        <property name="deptName" value="人力部"></property>
    </bean>

    <bean id="emp" class="com.kiong.spring6.Employee">
        <!--普通属性注入-->
        <property name="name" value="David"></property>
        <property name="age" value="21"></property>
        <!--级联注入-->
        <property name="dpt" ref="dept"></property>
        <property name="dpt.deptName" value="技术研发部"></property>
    </bean>
</beans>

方式3运行结果如下:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值