Spring Bean之间的关系
一、继承(指的是配置上的继承)
假设有一个Address类
package com.at.beans.relation;
public class Address {
private String city;
private String street;
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 + "]";
}
}
<?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="address" class="com.at.beans.relation.Address"
p:city="Beijing@@" p:street="WuDaoKou"></bean>
<bean id="address2" class="com.at.beans.relation.Address"
p:city="Beijing" p:street="SiDaoKou"></bean>
</beans>
测试代码
package com.at.beans.relation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAddress {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml");
Address address = (Address) ctx.getBean("address");
System.out.println(address);
Address address2 = (Address) ctx.getBean("address2");
System.out.println(address2);
}
}
五月 22, 2017 7:33:40 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@323f348a: startup date [Mon May 22 19:33:40 CST 2017]; root of context hierarchy
五月 22, 2017 7:33:40 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-relation.xml]
Address [city=Beijing@@, street=WuDaoKou]
Address [city=Beijing, street=SiDaoKou]
上面的程序虽然准确的输出了,但是我们发现在bean配置文件中,两个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"
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 id="address" class="com.at.beans.relation.Address"
p:city="Beijing@@" p:street="WuDaoKou"></bean>
<bean id="address2" p:city="Beijing" p:street="SiDaoKou" parent="address"></bean>
</beans>
(1)Spring 允许继承 bean 的配置, 被继承的 bean 称为父 bean. 继承这个父 Bean 的 Bean 称为子 Bean.
(2)子 Bean 从父 Bean 中继承配置, 包括 Bean 的属性配置。
(3)子 Bean 也可以覆盖从父 Bean 继承过来的配置。
(4)父 Bean 可以作为配置模板, 也可以作为 Bean 实例. 若只想把父 Bean 作为模板, 可以设置 <bean> 的abstract 属性为 true, 这样 Spring 将不会实例化这个 Bean。
(5)并不是 <bean> 元素里的所有属性都会被继承. 比如: autowire, abstract 等。
(6)也可以忽略父 Bean 的 class 属性, 让子 Bean 指定自己的类, 而共享相同的属性配置. 但此时 abstract 必须设为 true 。
二、依赖
使用上文中继承的Address类,除了这个类,还有Person类和Car类
Person类
package com.at.beans.relation;
public class Person {
private String name;
private Address address;
private Car car;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String toString() {
return "Person [name=" + name + ", address=" + address + ", car=" + car
+ "]";
}
}
package com.at.beans.relation;
public class Car {
private String brand;
private double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
<?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 id="address" class="com.at.beans.relation.Address"
p:city="Beijing@@" p:street="WuDaoKou"></bean>
<bean id="car" class="com.at.beans.relation.Car"
p:brand="Audi" p:price="400000"></bean>
<!-- 我们要求在配置bean时候,每一个person必须有一个关联的car,也即是说person这个bean依赖于car这个bean,这个car的bean会在本bean实例化之前创建好 -->
<bean id="person" class="com.at.beans.relation.Person"
p:name="luoyepiaoxue2014" p:address-ref="address" depends-on="car">
<property name="car" ref="car"></property>
</bean>
</beans>
测试函数如下
package com.at.beans.relation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAddress {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml");
Address address = (Address) ctx.getBean("address");
System.out.println(address);
Car car = (Car) ctx.getBean("car");
System.out.println(car);
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}
}
运行结果如下
五月 22, 2017 8:06:33 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@725b1426: startup date [Mon May 22 20:06:33 CST 2017]; root of context hierarchy
五月 22, 2017 8:06:33 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-relation.xml]
Address [city=Beijing@@, street=WuDaoKou]
Car [brand=Audi, price=400000.0]
Person [name=luoyepiaoxue2014, address=Address [city=Beijing@@, street=WuDaoKou], car=Car [brand=Audi, price=400000.0]]
(1)Spring 允许用户通过 depends-on 属性设定 bean前置依赖的bean,前置依赖的 bean会在本 bean实例化之前创建好。
(2)如果前置依赖于多个 bean,则可以通过逗号,空格或的方式配置 bean的名称。
By luoyepiaoxue2014
微博地址: http://weibo.com/luoyepiaoxue2014 点击打开链接