In this tutorial we are going to see about Spring Bean Definition Inheritance with example program.
A bean definition can contain a lot of configuration information, including constructor arguments, property values, and container-specific information such as initialization method, static factory method name, and so on. A child bean definition inherits configuration data from a parent definition. The child definition can override some values, or add others, as needed. Using parent and child bean definitions can save a lot of typing. Effectively, this is a form of templating.
If you work with an ApplicationContext interface programmatically, child bean definitions are represented by the ChildBeanDefinition class. Most users do not work with them on this level, instead configuring bean definitions declaratively in something like the ClassPathXmlApplicationContext. When you use XML-based configuration metadata, you indicate a child bean definition by using the parent attribute, specifying the parent bean as the value of this attribute.
Parent Bean with Class
A child bean definition uses the bean class from the parent definition if none is specified, but can also override it. In the latter case, the child bean class must be compatible with the parent, that is, it must accept the parent's property values.
A child bean definition inherits constructor argument values, property values, and method overrides from the parent, with the option to add new values. Any initialization method, destroy method, and/or static factory method settings that you specify will override the corresponding parent settings.
The remaining settings are always taken from the child definition: depends on, autowire mode, dependency check, singleton, scope, lazy init.
Parent Bean without Class
The preceding example explicitly marks the parent bean definition as abstract by using the abstract attribute. If the parent definition does not specify a class, explicitly marking the parent bean definition as abstract is required, as follows:
The parent bean cannot be instantiated on its own because it is incomplete, and it is also explicitly marked as abstract. When a definition is abstract like this, it is usable only as a pure template bean definition that serves as a parent definition for child definitions. Trying to use such an abstract parent bean on its own, by referring to it as a ref property of another bean or doing an explicit getBean() call with the parent bean id, returns an error. Similarly, the container's internal preInstantiateSingletons() method ignores bean definitions that are defined as abstract.
Note:
ApplicationContext pre-instantiates all singletons by default. Therefore, it is important (at least for singleton beans) that if you have a (parent) bean definition which you intend to use only as a template, and this definition specifies a class, you must make sure to set the abstract attribute to true, otherwise the application context will actually (attempt to) pre-instantiate the abstract bean.
Step by step procedure to create Spring Program using Eclipse given below.
Follow the Simple steps.
1. Select File –> New –> Java Project from your Eclipse IDE.
2.Create project called SpringBeanDefinitionInheritance.
3. Add the following jars into your build path.
5. Next create ParentBean class under com.javatutorialscorner.spring package
ParentBean.java
6. Next create ChildBean class under com.javatutorialscorner.spring package
ChildBean.java
7. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file
RunApp.java
In above program
First step I created Application context using ClassPathXmlApplicationContext which load configuration from bean file which located in class path of application, It take care of creating and initializing all the objects (Beans) declared in bean.xml
Next getBean() method used to get particular bean from created context. This method used bean name as parameter and returns generic object. We can caste to actual object.
Once Object Created you can access any method from that class.
7. Create Bean.xml at where your class files created (ClassPathXmlApplicationContext tries to load bean file from class path).
Bean.xml
Bean.xml is default name given to bean configuration file. You can choose any name for your bean.xml but you can use file name in main application to create context and your file available in your class path must be same.
Bean.xml is used to assign unique ID to different beans and controls the creation object with different values, using above file you can pass any value to variables without changing the class file.
Now you can run the program see the following output in console.
A bean definition can contain a lot of configuration information, including constructor arguments, property values, and container-specific information such as initialization method, static factory method name, and so on. A child bean definition inherits configuration data from a parent definition. The child definition can override some values, or add others, as needed. Using parent and child bean definitions can save a lot of typing. Effectively, this is a form of templating.
If you work with an ApplicationContext interface programmatically, child bean definitions are represented by the ChildBeanDefinition class. Most users do not work with them on this level, instead configuring bean definitions declaratively in something like the ClassPathXmlApplicationContext. When you use XML-based configuration metadata, you indicate a child bean definition by using the parent attribute, specifying the parent bean as the value of this attribute.
Parent Bean with Class
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 http://www.springframework.org/schema/beans/spring-beans.xsd"
>
05.
06.
<
bean
id
=
"inheritedTestBean"
abstract
=
"true"
07.
class
=
"com.javatutorialscorner.spring.beans.TestBean"
>
08.
<
property
name
=
"name"
value
=
"parent"
/>
09.
<
property
name
=
"age"
value
=
"1"
/>
10.
</
bean
>
11.
12.
<
bean
id
=
"inheritsWithDifferentClass"
class
=
"com.javatutorialscorner.spring.beans.DerivedTestBean"
13.
parent
=
"inheritedTestBean"
init-method
=
"initialize"
>
14.
15.
<
property
name
=
"name"
value
=
"override"
/>
16.
<!-- the age property value of 1 will be inherited from parent -->
17.
18.
</
bean
>
19.
</
beans
>
A child bean definition uses the bean class from the parent definition if none is specified, but can also override it. In the latter case, the child bean class must be compatible with the parent, that is, it must accept the parent's property values.
A child bean definition inherits constructor argument values, property values, and method overrides from the parent, with the option to add new values. Any initialization method, destroy method, and/or static factory method settings that you specify will override the corresponding parent settings.
The remaining settings are always taken from the child definition: depends on, autowire mode, dependency check, singleton, scope, lazy init.
Parent Bean without Class
The preceding example explicitly marks the parent bean definition as abstract by using the abstract attribute. If the parent definition does not specify a class, explicitly marking the parent bean definition as abstract is required, as follows:
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 http://www.springframework.org/schema/beans/spring-beans.xsd"
>
05.
06.
<
bean
id
=
"inheritedTestBean"
abstract
=
"true"
>
07.
<
property
name
=
"name"
value
=
"parent"
/>
08.
<
property
name
=
"age"
value
=
"1"
/>
09.
</
bean
>
10.
11.
<
bean
id
=
"inheritsWithDifferentClass"
class
=
"com.javatutorialscorner.spring.beans.DerivedTestBean"
12.
parent
=
"inheritedTestBean"
init-method
=
"initialize"
>
13.
14.
<
property
name
=
"name"
value
=
"override"
/>
15.
<!-- the age property value of 1 will be inherited from parent -->
16.
17.
</
bean
>
18.
</
beans
>
The parent bean cannot be instantiated on its own because it is incomplete, and it is also explicitly marked as abstract. When a definition is abstract like this, it is usable only as a pure template bean definition that serves as a parent definition for child definitions. Trying to use such an abstract parent bean on its own, by referring to it as a ref property of another bean or doing an explicit getBean() call with the parent bean id, returns an error. Similarly, the container's internal preInstantiateSingletons() method ignores bean definitions that are defined as abstract.
Note:
ApplicationContext pre-instantiates all singletons by default. Therefore, it is important (at least for singleton beans) that if you have a (parent) bean definition which you intend to use only as a template, and this definition specifies a class, you must make sure to set the abstract attribute to true, otherwise the application context will actually (attempt to) pre-instantiate the abstract bean.
Step by step procedure to create Spring Program using Eclipse given below.
Follow the Simple steps.
1. Select File –> New –> Java Project from your Eclipse IDE.
2.Create project called SpringBeanDefinitionInheritance.
3. Add the following jars into your build path.
4. Now create package com.javatutorialscorner.spring under SpringBeanDefinitionInheritance Project
commons-logging-1.1.3.jar
spring-beans-3.2.6.RELEASE.jar
spring-context-3.2.6.RELEASE.jar
spring-context-support-3.2.6.RELEASE.jar
spring-core-3.2.6.RELEASE.jar
spring-expression-3.2.6.RELEASE.jar
5. Next create ParentBean class under com.javatutorialscorner.spring package
ParentBean.java
01.
package
com.javatutorialscorner.spring;
02.
03.
public
class
ParentBean {
04.
private
String sayHello1;
05.
private
String sayHello2;
06.
07.
public
void
getSayHello1() {
08.
System.out.println(
"Java Tutorials Corner ParentBean getSayHello1 "
09.
+ sayHello1);
10.
}
11.
12.
public
void
setSayHello1(String sayHello1) {
13.
this
.sayHello1 = sayHello1;
14.
}
15.
16.
public
void
getSayHello2() {
17.
System.out.println(
"Java Tutorials Corner ParentBean getSayHello2 "
18.
+ sayHello2);
19.
}
20.
21.
public
void
setSayHello2(String sayHello2) {
22.
this
.sayHello2 = sayHello2;
23.
}
24.
25.
}
6. Next create ChildBean class under com.javatutorialscorner.spring package
ChildBean.java
01.
package
com.javatutorialscorner.spring;
02.
03.
public
class
ChildBean {
04.
private
String sayHello1;
05.
private
String sayHello2;
06.
private
String sayHello3;
07.
08.
public
void
getSayHello1() {
09.
System.out.println(
"Java Tutorials Corner ChildBean getSayHello1 "
10.
+ sayHello1);
11.
}
12.
13.
public
void
setSayHello1(String sayHello1) {
14.
this
.sayHello1 = sayHello1;
15.
}
16.
17.
public
void
getSayHello2() {
18.
System.out.println(
"Java Tutorials Corner ChildBean getSayHello2 "
19.
+ sayHello2);
20.
}
21.
22.
public
void
setSayHello2(String sayHello2) {
23.
this
.sayHello2 = sayHello2;
24.
}
25.
26.
public
void
getSayHello3() {
27.
System.out.println(
"Java Tutorials Corner ChildBean getSayHello3 "
28.
+ sayHello3);
29.
}
30.
31.
public
void
setSayHello3(String sayHello3) {
32.
this
.sayHello3 = sayHello3;
33.
}
34.
35.
}
7. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file
RunApp.java
01.
package
com.javatutorialscorner.spring;
02.
03.
import
org.springframework.context.support.AbstractApplicationContext;
04.
import
org.springframework.context.support.ClassPathXmlApplicationContext;
05.
06.
public
class
RunApp {
07.
08.
/**
09.
* @param args
10.
*/
11.
public
static
void
main(String[] args) {
12.
// TODO Auto-generated method stub
13.
14.
AbstractApplicationContext context =
new
ClassPathXmlApplicationContext(
15.
"Beans.xml"
);
16.
17.
ParentBean parentBean = (ParentBean) context.getBean(
"beanTemplate"
);
18.
19.
parentBean.getSayHello1();
20.
parentBean.getSayHello2();
21.
22.
ChildBean childBean = (ChildBean) context.getBean(
"inheritedBean"
);
23.
24.
childBean.getSayHello1();
25.
childBean.getSayHello2();
26.
childBean.getSayHello3();
27.
28.
}
29.
30.
}
In above program
First step I created Application context using ClassPathXmlApplicationContext which load configuration from bean file which located in class path of application, It take care of creating and initializing all the objects (Beans) declared in bean.xml
Next getBean() method used to get particular bean from created context. This method used bean name as parameter and returns generic object. We can caste to actual object.
Once Object Created you can access any method from that class.
7. Create Bean.xml at where your class files created (ClassPathXmlApplicationContext tries to load bean file from class path).
Bean.xml
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 http://www.springframework.org/schema/beans/spring-beans.xsd"
>
05.
06.
<
bean
id
=
"beanTemplate"
class
=
"com.javatutorialscorner.spring.ParentBean"
>
07.
<
property
name
=
"sayHello1"
value
=
"Spring Tutorial"
/>
08.
<
property
name
=
"sayHello2"
value
=
"Web Service Tutorial"
/>
09.
</
bean
>
10.
11.
<
bean
id
=
"inheritedBean"
class
=
"com.javatutorialscorner.spring.ChildBean"
12.
parent
=
"beanTemplate"
>
13.
14.
<
property
name
=
"sayHello1"
value
=
"JSON Tutorial"
/>
15.
<
property
name
=
"sayHello2"
value
=
"XML Parsing Tutorial"
/>
16.
<
property
name
=
"sayHello3"
value
=
"Servlet Tutorial"
/>
17.
</
bean
>
18.
</
beans
>
Bean.xml is default name given to bean configuration file. You can choose any name for your bean.xml but you can use file name in main application to create context and your file available in your class path must be same.
Bean.xml is used to assign unique ID to different beans and controls the creation object with different values, using above file you can pass any value to variables without changing the class file.
Now you can run the program see the following output in console.
Java Tutorials Corner ParentBean getSayHello1 Spring Tutorial
Java Tutorials Corner ParentBean getSayHello2 Web Service Tutorial
Java Tutorials Corner ChildBean getSayHello1 JSON Tutorial
Java Tutorials Corner ChildBean getSayHello2 XML Parsing Tutorial
Java Tutorials Corner ChildBean getSayHello3 Servlet Tutorial
0 comments:
Post a Comment