本文中介绍的bean之间的关系(继承、依赖)并非类之间的关系,是指配置文件的继承及依赖。
继承
bean-relation.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 id="address" class="com.wusuiwei.spring.beans.relation.Address" p:city="BeiJing" p:street="WuDaoKou"></bean>
<bean id="address2" class="com.wusuiwei.spring.beans.relation.Address" p:city="BeiJing" p:street="TiAnMen"></bean>
-->
<!--
抽象bean:bean的abstract属性为true的bean.这样的bean不能被IOC容器实例化,只能被用来继承配置.
若某一个bean的class属性没有被指定,则该bean必须是一个抽象bean.
-->
<bean id="address" abstract="true" p:city="Beijing^" p:Street="DazhongSi" ></bean>
<bean id="address2" class="com.wusuiwei.spring.beans.relation.Address" parent="address"></bean>
<!-- Bean配置的继承:使用bean的parent属性指定继承哪个bean的配置-->
<!--
<bean id="address" class="com.wusuiwei.spring.beans.relation.Address" p:city="BeiJing" p:street="WuDaoKou"></bean>
<bean id="address2" parent="address" p:street="TiAnMen"></bean>
-->
</beans>
Main类(测试类)
package com.wusuiwei.spring.beans.relation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean-relation.xml");
Address address = (Address) context.getBean("address");
System.out.println(address);
Address address2 = (Address) context.getBean("address2");
System.out.println(address2);
}
}
Address类
package com.wusuiwei.spring.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 + '\'' +
'}';
}
}
依赖
bean-relation.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">
<!--依赖即Person类实例化之前需要实例化car,如果实例化car失败,则抛异常(org.springframework.beans.factory.NoSuchBeanDefinitionException)
如果需要添加多个依赖,则可以使用空格或逗号分隔.
-->
<bean id="car" class="com.wusuiwei.spring.beans.autowire.Car" p:price="30000" p:brand="Audi"></bean>
<bean id="person" class="com.wusuiwei.spring.beans.autowire.Person" depends-on="car"/>
</beans>
Main类(测试类)
package com.wusuiwei.spring.beans.relation;
import com.wusuiwei.spring.beans.autowire.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean-relation.xml");
Person person = (Person) context.getBean("person");
System.out.println(person);
}
}
Person类
package com.wusuiwei.spring.beans.autowire;
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;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", address=" + address +
", car=" + car +
'}';
}
}
Car类:
package com.wusuiwei.spring.beans.autowire;
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 +
'}';
}
}