spring6 依赖注入-对象属性类型

DiTest.java

package bean.ditest;

import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DiTest {
    @Test
    public void test(){

        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans-diTest.xml");
        Dept dept1 = (Dept) ac.getBean("dept1");
        dept1.info();
        System.out.println(dept1);

        Emp emp1 = (Emp) ac.getBean("emp1");
        emp1.work();
        System.out.println(emp1);
    }
}

 

beans-diTest.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="dept1" class="bean.ditest.Dept">
    <property name="dname" value="安保部门"></property>
</bean>

    <bean id="emp1" class="bean.ditest.Emp">
<!--        注入对象属性-->
        <property name="age" value="21"></property>
<!--        注入普通数据属性-->
        <property name="ename" value="吴危险"></property>
        <property name="dept" ref="dept1"></property>
    </bean>

</beans>

 两个实体类:

Emp.java

package bean.ditest;

public class Emp {
    private Dept dept;

    private String ename;
    private String age;

    public void work(){
        System.out.println(ename+"工作中"+",年龄:"+age);
        dept.info();
    }

    @Override
    public String toString() {
        return "Emp{" +
                "dept=" + dept +
                ", ename='" + ename + '\'' +
                ", age='" + age + '\'' +
                '}';
    }

    public Emp() {
    }

    public Emp(Dept dept, String ename, String age) {
        this.dept = dept;
        this.ename = ename;
        this.age = 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 String getAge() {
        return age;
    }

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

Dept.java

package bean.ditest;

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

    public String getDname() {
        return dname;
    }

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

    public Dept(String dname) {
        this.dname = dname;
    }

    public Dept() {
    }

    @Override
    public String toString() {
        return "Dept{" +
                "dname='" + dname + '\'' +
                '}';
    }
}

 控制台输出:

总结:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值