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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="inheritedTestBean" abstract="true"
class="com.javatutorialscorner.spring.beans.TestBean">
<property name="name" value="parent" />
<property name="age" value="1" />
</bean>
<bean id="inheritsWithDifferentClass" class="com.javatutorialscorner.spring.beans.DerivedTestBean"
parent="inheritedTestBean" init-method="initialize">
<property name="name" value="override" />
<!-- the age property value of 1 will be inherited from parent -->
</bean>
</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:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="inheritedTestBean" abstract="true">
<property name="name" value="parent" />
<property name="age" value="1" />
</bean>
<bean id="inheritsWithDifferentClass" class="com.javatutorialscorner.spring.beans.DerivedTestBean"
parent="inheritedTestBean" init-method="initialize">
<property name="name" value="override" />
<!-- the age property value of 1 will be inherited from parent -->
</bean>
</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
package com.javatutorialscorner.spring;
public class ParentBean {
private String sayHello1;
private String sayHello2;
public void getSayHello1() {
System.out.println("Java Tutorials Corner ParentBean getSayHello1 "
+ sayHello1);
}
public void setSayHello1(String sayHello1) {
this.sayHello1 = sayHello1;
}
public void getSayHello2() {
System.out.println("Java Tutorials Corner ParentBean getSayHello2 "
+ sayHello2);
}
public void setSayHello2(String sayHello2) {
this.sayHello2 = sayHello2;
}
}
6. Next create ChildBean class under com.javatutorialscorner.spring package
ChildBean.java
package com.javatutorialscorner.spring;
public class ChildBean {
private String sayHello1;
private String sayHello2;
private String sayHello3;
public void getSayHello1() {
System.out.println("Java Tutorials Corner ChildBean getSayHello1 "
+ sayHello1);
}
public void setSayHello1(String sayHello1) {
this.sayHello1 = sayHello1;
}
public void getSayHello2() {
System.out.println("Java Tutorials Corner ChildBean getSayHello2 "
+ sayHello2);
}
public void setSayHello2(String sayHello2) {
this.sayHello2 = sayHello2;
}
public void getSayHello3() {
System.out.println("Java Tutorials Corner ChildBean getSayHello3 "
+ sayHello3);
}
public void setSayHello3(String sayHello3) {
this.sayHello3 = sayHello3;
}
}
7. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file
RunApp.java
package com.javatutorialscorner.spring;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RunApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
"Beans.xml");
ParentBean parentBean = (ParentBean) context.getBean("beanTemplate");
parentBean.getSayHello1();
parentBean.getSayHello2();
ChildBean childBean = (ChildBean) context.getBean("inheritedBean");
childBean.getSayHello1();
childBean.getSayHello2();
childBean.getSayHello3();
}
}
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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanTemplate" class="com.javatutorialscorner.spring.ParentBean">
<property name="sayHello1" value="Spring Tutorial" />
<property name="sayHello2" value="Web Service Tutorial" />
</bean>
<bean id="inheritedBean" class="com.javatutorialscorner.spring.ChildBean"
parent="beanTemplate">
<property name="sayHello1" value="JSON Tutorial" />
<property name="sayHello2" value="XML Parsing Tutorial" />
<property name="sayHello3" value="Servlet Tutorial" />
</bean>
</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