-Spring允许继承bean的配置,被继承的bean成为父bean,继承这个父bean的称为子bean;
–子bean从父bean中继承配置,包括bean的属性配置;
–父bean可以做为配置模版,也可以作为Bean实例,若只想把父bean作为模版,可以设置的abstract属性为true,这样spring将不会实例化这个bean。
–并不是元素中的所以属性都会被继承,比如:autowire,abstract等。
–也可以忽略父bean的class属性,让子bean指定自己的类,而共享相同的配置,单此时abstract必须设为true。
例子:
Address类:
package com.xia.entity;
public class Address {
private String city;
private String street;
public Address() {
super();
// TODO Auto-generated constructor stub
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [city=" + city + ", street=" + street + "]";
}
}
配置文件:
<!-- 抽象bean:bean的abstract 属性为true时,这样的bean不能被IOC容器实例化,只用于被继承配置,
若某一个bean的class属性没有被指定,那么bean必须是一个抽象bean-->
<bean id="address" abstract="true">
<property name="city" value="BeiJing"></property>
<property name="street" value="wuDaoKou"></property>
</bean>
<bean id="address1" parent="address" class="com.xia.entity.Address">
<property name="street" value="daZhangSi"></property>
</bean>
测试:
ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
// Address address = (Address) ac.getBean("address");
// System.out.println(address);
Address address1 = (Address) ac.getBean("address1");
System.out.println(address1);
}
输出:
Address [city=BeiJing, street=daZhangSi]
–Spring允许用户通过depends-on属性设定Bean前置依赖的Bean,前置依赖的Bean会在本Bean实例化之前创建好;
–如果前置依赖于多个Bean,则可以通过逗号,空格或的方式配置Bean的名称。
例子:
添加两个类:
car:
package com.xia.entity;
public class Car {
private String cname;
private double price;
public Car(String cname, double price) {
super();
this.cname = cname;
this.price = price;
}
@Override
public String toString() {
return "Car [cname=" + cname + ", price=" + price + "]";
}
public Car() {
super();
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
person:
package com.xia.entity;
public class Person {
private String name;
private int age;
private Car car;
private Address address;
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", car=" + car + ", address=" + address + "]";
}
public Car getCar() {
return car;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public void setCar(Car car) {
this.car = car;
}
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;
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 抽象bean:bean的abstract 属性为true时,这样的bean不能被IOC容器实例化,只用于被继承配置,
若某一个bean的class属性没有被指定,那么bean必须是一个抽象bean-->
<bean id="address" abstract="true">
<property name="city" value="BeiJing"></property>
<property name="street" value="wuDaoKou"></property>
</bean>
<bean id="address1" parent="address" class="com.xia.entity.Address">
<property name="street" value="daZhangSi"></property>
</bean>
<bean id="car" class="com.xia.entity.Car">
<property name="cname" value="auto"></property>
<property name="price">
<value>1000000</value>
</property>
</bean>
<!-- 要求配置Person时,必须要有一个关联的car! 换句话说person这个Bean依赖于car这个Bean-->
<bean id="person" class="com.xia.entity.Person" depends-on="car">
<property name="name" value="Tom"></property>
<property name="age">
<value>18</value>
</property>
<property name="address">
<ref bean="address1"/>
</property>
</bean>
</beans>
测试类:
package com.xia.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xia.entity.Address;
import com.xia.entity.Person;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Person p = (Person) ac.getBean("person");
System.out.println(p);
}
}