This article in JavaRanch Big Moose Saloon prompted me to start eclipse and write some test code. The problem is very basic. How do you map a parent bean as a part of Spring Bean definition.
Let's take a look at the following three classes to see how they map using 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
}
We started to map them in 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>
Now, let's write a simple application to show what will happen when these mappings are used:
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
}
If we run this class, we will get the following results:
1
ID: 2, Name: superBean, Property: subBean
2
ID: 3, Name: superBean, Property: differentBeanwww.2cto.com
We learned from this example that if your bean class does not inherit other classes, you can use the parent attribute to map the definition of the parent bean. A required condition is that the subclass must be compatible with the parent class, that is, the subclass must have all attributes defined in the parent class. In fact, the official Spring document says that you do not need to declare a new object inherited from the parent class.
I found it useful to use the parent class definition as a template. We can use it as an Object-oriented Inheritance, another idea is to use it as the parent interface of the subclass. You must inherit this to use its definition.
Author: Bairrfhoinn