03-Spring5用xml为bean注入bean

1.编写测试类

User是用户类, Dept是部门类, 用户与部门的关系为多对一. 要求是往User里面注入Dept
在这里插入图片描述

package com.limi.test;

public class User {
    private String name;

    private Integer age;

    private String  hoby;

    private Dept dept;

    public User(){

    }
    public User(String name, Integer age){
        this.name = name;
        this.age = age;
    }

    public void say(){
        System.out.println("name="+name+" age="+age+" hoby="+hoby);
        System.out.println("\n"+dept.getNo()+" "+dept.getName()+" "+dept.getLevel());
    }

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

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

    public void setHoby(String hoby) {
        this.hoby = hoby;
    }

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

}


package com.limi.test;

public class Dept {

    private Long no;        //编号

    private String name;    //名称

    private Integer level; //等级

    public void setNo(Long no) {
        this.no = no;
    }

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

    public void setLevel(Integer level) {
        this.level = level;
    }

    public Long getNo() {
        return no;
    }

    public String getName() {
        return name;
    }

    public Integer getLevel() {
        return level;
    }
}


package com.limi.test;

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

public class MyTest {

    @Test
    public void test1(){
        //1.加载bean的xml文件, 以src为根目录
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");

        //2.获取配置的对象, 参数1:bean的id值, 参数2: 类名.class
        User user = context.getBean("User", User.class);

        //3.使用User对象
        user.say();
    }
}

2.第一种注入方式:外部bean注入

  • 要注入的Dept bean标签写在User 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">
    <!--id是唯一标识, class是全类名    -->
    <bean id="User" class="com.limi.test.User">
       <property name="name" value="andy"></property>
        <!-- 注入外部bean-->
        <property name="dept" ref="Dept"></property>
        <!--可以对引入的bean进行修改, 但是User类中一定要有dept的getter方法-->
        <property name="dept.no" value="1111"></property>
        <property name="dept.level" value="6666"></property>
    </bean>

    <!--Dept bean写在User bean的外部-->
    <bean id="Dept" class="com.limi.test.Dept">
        <property name="no" value="1"></property>
        <property name="name" value="计算机学院"></property>
     </bean>

</beans>
  • 测试结果
    在这里插入图片描述

3.第二种注入方式:内部bean注入

  • 要注入的Dept bean标签写在User 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">
    <!--id是唯一标识, class是全类名    -->
    <bean id="User" class="com.limi.test.User">
       <property name="name" value="andy"></property>
        <!-- 注入内部bean-->
        <property name="dept">
            <!--Dept bean写在User bean的内部-->
            <bean id="Dept" class="com.limi.test.Dept">
                <property name="no" value="1"></property>
                <property name="name" value="计算机学院"></property>
                <property name="level" value="666"></property>
            </bean>
        </property>
    </bean>
</beans>
  • 测试结果
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值