在Spring中使用parent bean定义
在 JavaRanch Big Moose Saloon 的 这篇文章促使我启动 eclipse 并且写下了壹些测试代码。 问题非常基础,你如何去映射壹个 parent bean 作为 Spring 的 Bean 定义的壹部分。
我们壹起来看看以下三个类,看它们如何使用 Spring 来映射。
01
public abstract class SuperBean {
02
private int id;
03
private String name;
04
05
public int getId() {
06
return id;
07
}
08
09
public void setId(int id) {
10
this.id = id;
11
}
12
13
public String getName() {
14
return name;
15
}
16
17
public void setName(String name) {
18
this.name = name;
19
}
20
}
21
22
public class SubBean extends SuperBean {
23
private String newProperty;
24
25
public String getNewProperty() {
26
return newProperty;
27
}
28
29
public void setNewProperty(String newProperty) {
30
this.newProperty = newProperty;
31
}
32
}
33
34
public class DifferentBean {
35
private int id;
36
private String name;
37
private String myProperty;
38
39
public int getId() {
40
return id;
41
}
42
43
public void setId(int id) {
44
this.id = id;
45
}
46
47
public String getName() {
48
return name;
49
}
50
51
public void setName(String name) {
52
this.name = name;
53
}
54
55
public String getMyProperty() {
56
return myProperty;
57
}
58
59
public void setMyProperty(String myProperty) {
60
this.myProperty = myProperty;
61
}
62
}
我们开始在 Spring 中来映射它们:
01
<?xml version="1.0" encoding="UTF-8"?>
02
<beans xmlns="http://www.springframework.org/schema/beans"
03
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04
xsi:schemaLocation="http://www.springframework.org/schema/beans
05
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
06
07
<bean id="superBean" abstract="true"
08
class="com.stevideter.spring.SuperBean">
09
<property name="id" value="1" />
10
<property name="name" value="superBean" />
11
</bean>
12
13
<bean id="subBean" class="com.stevideter.spring.SubBean"
14
parent="superBean">
15
<property name="id" value="2" />
16
<property name="newProperty" value="subBean" />
17
</bean>
18
19
<bean id="differentBean" class="com.stevideter.spring.DifferentBean"
20
parent="superBean">
21
<property name="id" value="3" />
22
<property name="myProperty" value="differentBean" />
23
</bean>
24
</beans>
并且现在我们写壹个简单的应用来展示使用这些映射到底会发生什么情况:
01
import org.springframework.context.ApplicationContext;
02
import org.springframework.context.support.ClassPathXmlApplicationContext;
03
04
public class TestClass {
05
public static void main(String[] args) {
06
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
07
SubBean subBean = (SubBean)context.getBean("subBean");
08
System.out.printf("ID: %d, Name: %s, Property: %s\n",
09
subBean.getId(), subBean.getName(), subBean.getNewProperty());
10
DifferentBean differentBean = (DifferentBean)context.getBean("differentBean");
11
System.out.printf("ID: %d, Name: %s, Property: %s\n",
12
differentBean.getId(), differentBean.getName(), differentBean.getMyProperty());
13
}
14
}
如果我们运行这个类,会获取如下结果:
1
ID: 2, Name: superBean, Property: subBean
2
ID: 3, Name: superBean, Property: differentBeanwww.zzzyk.com
我们从这个例子中了解到,如果你的 bean 类没有继承别的类,可以使用 parent 属性来映射 parent bean 的定义。壹个必要条件是子类必须与父类保持兼容,也就是说,子类必须具有父类中已经定义过的所有属性。事实上, Spring 官方文档 说你完全不需要声明壹个继承自父类的新对象。
我发现将父类定义作为壹个模板的方法很有用,我们可以将其作为壹个面向对象风格的继承,另外壹种想法就是将其作为子类的父接口,你必须继承这些发生以便使用其定义。
作者:Bairrfhoinn
补充:软件开发 , Java ,