第六讲:spring管理的bean之间的关系

一,继承关系
1,新建两个包,三个类:

6,spring管理的bean之间的关系
People类如下:
package com.cruise.entity;

public  class People {

    private int id;
    private String name;
    private int age;
    private String className;

    public int getId() {
       return id;
    }
    public void setId(int id) {
       this.id = id;
    }
    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 String getClassName() {
       return className;
    }
    public void setClassName(String className) {
       this.className = className;
    }
    @Override
    public String toString() {
       return "People [id=" + id + ", name=" + name + ", age=" + age + ", className=" + className + "]";
    }
}
beans.xml文件如下:定义一个抽象类,指向一个People,zhangsan和lisi的父类都是abstractPeople,但是lisi重写了一下age字段。
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="abstractPeople" class="com.cruise.entity.People" abstract="true">
    <property name="className" value="高二(1)班">property>
    <property name="age" value="18">property>
bean>

<bean id="zhangsan" parent="abstractPeople">
    <property name="id" value="1">property>
    <property name="name" value="张三">property>
bean>

<bean id="lisi" parent="abstractPeople">
    <property name="id" value="1">property>
    <property name="name" value="李四">property>
    <property name="age" value="21">property>
bean>

beans>
T类测试代码如下:
package com.cruise.test;

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

import com.cruise.entity.People;

public class T {

    private ClassPathXmlApplicationContext CPXAC=null;
    @Before
    public void setUp() throws Exception {
       CPXAC= new ClassPathXmlApplicationContext("beans.xml");
    }

    @Test
    public void test1() {
       
       People  zhangsan = (People) CPXAC.getBean("zhangsan");
       System.out.println(zhangsan);
       People  lisi = (People) CPXAC.getBean("lisi");
       System.out.println(lisi);
    }
}

二,依赖关系
需求:在查看zhangsan信息的时候,需要有权限信息,
1. 新建com.cruise.service 包,和 Authority类

6,spring管理的bean之间的关系
Authority类如下,只有一个构造函数,作为模拟测试使用:
package com.cruise.service;

public class Authority {

    public Authority() {
       System.out.println("获取权限");
    }
}

2. People类生成无参构造方法,作为模拟测试使用
package com.cruise.entity;

public  class People {

    private int id;
    private String name;
    private int age;
    private String className;
    
    public People() {
       System.out.println("张三");
    }
    public int getId() {
       return id;
    }
    public void setId(int id) {
       this.id = id;
    }
    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 String getClassName() {
       return className;
    }
    public void setClassName(String className) {
       this.className = className;
    }
    @Override
    public String toString() {
       return "People [id=" + id + ", name=" + name + ", age=" + age + ", className=" + className + "]";
    }
}
3. 写beans.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="abstractPeople" class="com.cruise.entity.People" abstract="true">
    <property name="className" value="高二(1)班">property>
    <property name="age" value="18">property>
bean>

<bean id="zhangsan" parent="abstractPeople" depends-on="authority">
    <property name="id" value="1">property>
    <property name="name" value="张三">property>
bean>

<bean id="lisi" parent="abstractPeople">
    <property name="id" value="1">property>
    <property name="name" value="李四">property>
    <property name="age" value="21">property>
bean>

<bean id="authority" class="com.cruise.service.Authority">bean>

beans>
4. 测试代码:
package com.cruise.test;

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

import com.cruise.entity.People;

public class T {

    private ClassPathXmlApplicationContext CPXAC=null;
    @Before
    public void setUp() throws Exception {
       CPXAC= new ClassPathXmlApplicationContext("beans.xml");
    }

    @Test
    public void test1() {
       
       People  zhangsan = (People) CPXAC.getBean("zhangsan");
       System.out.println(zhangsan);
       People  lisi = (People) CPXAC.getBean("lisi");
       System.out.println(lisi);
    }
}
5. 测试结果分析,通常在加载beans.xml的时候,按顺序加载,但是有了depends-on在初始化zhangsan的时候,首先要执行Authority类,通过构造方法的执行顺序来判定:
三,引用关系,引用关系之前其实已经演示了,这里就不演示了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值