一,继承关系
1,新建两个包,三个类:
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类
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类,通过构造方法的执行顺序来判定:
三,引用关系,引用关系之前其实已经演示了,这里就不演示了。
第六讲:spring管理的bean之间的关系
最新推荐文章于 2020-06-22 21:55:35 发布