04 Bean之间的关系

继承&依赖

1. 继承

先看一个例子:

address.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package relation;/*
* @Author Sheng WenZeng
* @Date 2019/8/10 23:30
* @Version 1.0
*/

/**
* @author Sheng Wenzeng
* @ClassName Address
* @Description TODO
* @Date 2019/8/10 23:30
* @Version 1.0
*/
public class Address {
private String city;
private String street;

@Override
public String toString() {
return "Address{" +
"city='" + city + '\'' +
", street='" + street + '\'' +
'}';
}

public void setCity(String city) {
this.city = city;
}

public void setStreet(String street) {
this.street = street;
}
}

Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package relation;/*
* @Author Sheng WenZeng
* @Date 2019/8/10 23:31
* @Version 1.0
*/

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author Sheng Wenzeng
* @ClassName Main
* @Description TODO
* @Date 2019/8/10 23:31
* @Version 1.0
*/
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Address address = (Address) applicationContext.getBean("address");
System.out.println(address);
Address address_1 = (Address) applicationContext.getBean("address_1");
System.out.println(address_1);
}
}

applicationContext.xml

1
2
3
4
5
6
7
<?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="relation.Address" p:city="Beijing" p:street="NanJingLu"/>
<bean id="address_1" class="relation.Address" p:city="HeFei" p:street="NanJingLu"/>
</beans>

运行结果为

1
2
Address{city='Beijing', street='NanJingLu'}
Address{city='HeFei', street='NanJingLu'}

可以发现其中配置的两个bean基本相同,因此第二个bean可以继承第一个bean,将applicationContext.xml修改为以下内容:

1
2
3
4
5
6
7
<?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="relation.Address" p:city="Beijing" p:street="NanJingLu"/>
<bean id="address_1" parent="address" p:city="HeFei"/>
</beans>

结果相同

  • Spring允许继承Bean的配置,被继承的Bean成为父Bean.继承这个Bean的Bean成为子Bean
  • 子Bean从父Bean中继承配置,包括Bean的属性配置
  • 子Bean可以覆盖父Bean继承来的配置
  • 父Bean可以作为配置模板,也可以作为Bean实例.若只想把父Bean作为模板,可以设置<bean>的abstract属性为true,这样Sprint将不会实例化这个Bean
  • 并不是<bean>中所有属性都会被继承,比如:autowire,abstract等
  • 也可以忽略父Bean的class属性,让子Bean制定自己的类,而共享相同的属性配置.但此时abstract必须设置为true

2. 依赖

Spring允许用户通过depend-on属性设定Bean前置依赖的Bean,前置依赖的Bean会在本Bean实例化之前创建好

如果前置依赖于多个Bean,则可以通过逗号空格的方式配置Bean的名称

例:

Person.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package relation;/*
* @Author Sheng WenZeng
* @Date 2019/8/11 0:03
* @Version 1.0
*/

/**
* @author Sheng Wenzeng
* @ClassName Person
* @Description TODO
* @Date 2019/8/11 0:03
* @Version 1.0
*/
public class Person {
private String name;
private Address address;
private Car car;

@Override
public String toString() {
return "person{" +
"name='" + name + '\'' +
", address=" + address +
", car=" + car +
'}';
}

public void setCar(Car car) {
this.car = car;
}

public void setName(String name) {
this.name = name;
}

public void setAddress(Address address) {
this.address = address;
}
}

Address.java同上

Car.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package relation;/*
* @Author Sheng WenZeng
* @Date 2019/8/11 0:09
* @Version 1.0
*/

/**
* @author Sheng Wenzeng
* @ClassName Car
* @Description TODO
* @Date 2019/8/11 0:09
* @Version 1.0
*/
public class Car {
private String brand;
private int speed;
private double price;

@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", speed=" + speed +
", price=" + price +
'}';
}

public void setBrand(String brand) {
this.brand = brand;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public void setPrice(double price) {
this.price = price;
}
}

Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package relation;/*
* @Author Sheng WenZeng
* @Date 2019/8/10 23:31
* @Version 1.0
*/

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author Sheng Wenzeng
* @ClassName Main
* @Description TODO
* @Date 2019/8/10 23:31
* @Version 1.0
*/
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) applicationContext.getBean("person");
System.out.println(person);
}
}

applicationContext.xml

1
2
3
4
5
6
7
8
<?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="relation.Address" p:city="Beijing" p:street="NanJingLu"/>
<bean id="address_1" parent="address" p:city="HeFei"/>
<bean id="person" class="relation.person" p:name="Tom" p:address-ref="address"/>
</beans>

输出:

1
person{name='Tom', address=Address{city='Beijing', street='NanJingLu'}, car=null}

如果要求person这个Bean一定要绑定一个Car,即person依赖于Car:

1
2
3
4
5
6
7
8
9
<?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="relation.Address" p:city="Beijing" p:street="NanJingLu"/>
<bean id="address_1" parent="address" p:city="HeFei"/>
<bean id="car" class="relation.Car" p:brand="Audi" p:price="100000" p:speed="200"/>
<bean id="person" class="relation.Person" p:name="Tom" p:address-ref="address" depends-on="car"/>
</beans>

运行结果和之前仍然相同,但是已经这个id为car的Bean已经在person实例化之前被实例化了,只是car不属于person,所以person的car仍然为null

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×