Spring Bean之间的关系

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;}@Overridepublic String toString() {return "Address [city=" + city + ", street=" + street + "]";}}


配置文件如下




测试代码

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有相同的地方,所以我们可以通过如下继承的配置文件来优化:




继承总结:

(1)Spring 允许继承 bean 的配置, 被继承的 bean 称为父 bean. 继承这个父 Bean 的 Bean 称为子 Bean.
(2)子 Bean 从父 Bean 中继承配置, 包括 Bean 的属性配置。
(3)子 Bean 也可以覆盖从父 Bean 继承过来的配置。
(4)父 Bean 可以作为配置模板, 也可以作为 Bean 实例. 若只想把父 Bean 作为模板, 可以设置 的abstract 属性为 true, 这样 Spring 将不会实例化这个 Bean。
(5)并不是 元素里的所有属性都会被继承. 比如: 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+ "]";}}


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;}@Overridepublic String toString() {return "Car [brand=" + brand + ", price=" + price + "]";}
}


我们要求在配置bean时候,每一个person必须有一个关联的car,也即是说person这个bean依赖于car这个bean,配置文件如下


		


测试函数如下

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 点击打开链接


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部