继承Bean配置
- Spring允许继承bean的配置 ,被继承的bean称为父bean,继承这个父Bean的Bean称为子Bean
- 子Bean从父Bean中继承配置,包括Bean的属性配置
- 子Bean也可以 覆盖 从父Bean继承过来的配置
- 父Bean可以作为配置模版,也可以作为Bean实例, 若只想把父Bean作为模版,可以配置<bean>的abstract属性为true ,这样Spring将不会实例化这个Bean
- 并不是<bean>元素里的所有属性都会被继承 。比如:autowire,abstract等。
- 也 可以忽略父Bean的class属性 ,让子Bean指定自己的类,而共享相同的属性配置,但此时 abstract必须设为true
依赖Bean配置
- Spring允许用户通过depends-on属性设定Bean前置依赖的Bean ,前置依赖的Bean会在本Bean实例化之前创建好
- 如果前置依赖于多个Bean,则可以通过逗号,空格或的方式配置Bean的名称
实例代码详解
package com.spring.auto.wire;
public class Department {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Department [name=" + name + "]";
}
}
package com.spring.auto.wire;
public class Company {
private String name;
private Department department;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Company [name=" + name + ", department=" + department + "]";
}
}
package com.spring.auto.wire;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main_relation {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml");
Department department = (Department) ctx.getBean("department2");
System.out.println(department);
Company company = (Company) ctx.getBean("company");
System.out.println(company);
Company company1 = (Company) ctx.getBean("company1");
System.out.println(company1);
Company company2 = (Company) ctx.getBean("company2");
System.out.println(company2);
}
}
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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 抽象bean:bean的abstract属性为true的bean。这样bean不能被IOC容器实例化,只能用来被继承继承 若某一个bean的class属性没有指定,则该bean必须是一个抽象bean--> <bean id="department1" p:name="JKY" abstract="true"></bean> <!-- bean 配置的继承:使用bean 的parent 属性指定继承哪个baen的配置 --> <bean id="department2" class="com.spring.auto.wire.Department" parent="department1"></bean> <bean id="company" class="com.spring.auto.wire.Company" p:name="TT" p:department-ref="department2"></bean> <!-- 要求在配置company1时,必须有一个关联的department2!换句话说company这个bean依赖于department2这个bean. 注: 这里的依赖是指在实例化这个bean之前,必须实例化依赖的bean. 注意与ref的区别 --> <bean id="company1" class="com.spring.auto.wire.Company" p:name="TT1" depends-on="department2" p:department-ref="department2"></bean> <bean id="company2" class="com.spring.auto.wire.Company" p:name="TT1" depends-on="company"></bean> </beans>运行结果:
Department [name=JKY]
Company [name=TT, department=Department [name=JKY]]
Company [name=TT1, department=Department [name=JKY]]
Company [name=TT1, department=null]
注意: depends-on 与 ref的区别,见:
http://stackoverflow.com/questions/12663491/ref-vs-depends-on-attributes-in-spring