Spring bean inheritance configuration
A spring bean definition contains lot of information like property values, constructor arguments, and container specific information like init and
destroy method settings and so on. Spring allows to inherit all these bean properties. A child bean definition can inherit configurations from its parent definition.
The child bean definition can override some values, or add new values.
This example shows how to inherit parent bean values with in child bean. We have Employee class, the parent bean sets the common value for the property called
company. Any other bean definitions will inherits it.
package com.java2novice.beans;
public class Employee {
private int empId;
private String name;
private String company;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@Override
public String toString(){
return this.empId+" | "+this.name+" | "+this.company;
}
}
|
Here is the xml based configurations, you can see the bean reference "baseEmployeeBean" playing as parent bean and bean reference "myEmployeeBean" playing as
child bean and inheriting property called company, and adding new properties empId and name.
<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-3.0.xsd">
<bean id="baseEmployeeBean" class="com.java2novice.beans.Employee">
<property name="company" value="Java2novice" />
</bean>
<bean id="myEmployeeBean" parent="baseEmployeeBean">
<property name="empId" value="1016" />
<property name="name" value="Nataraja Gootooru" />
</bean>
</beans>
|
Spring bean demo class:
package com.java2novice.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2novice.beans.Employee;
public class SpringDemo {
public static void main(String a[]){
String confFile = "applicationContext.xml";
ConfigurableApplicationContext context
= new ClassPathXmlApplicationContext(confFile);
Employee myEmp = (Employee) context.getBean("myEmployeeBean");
System.out.println(myEmp.toString());
}
}
|
Output: |
1016 | Nataraja Gootooru | Java2novice
|
Abstract attribute in parent bean
With the above given xml based configuration, you can get object for even parent bean. Spring allows you to restrict parent bean by specifying abstract property.
Below configuration makes the parent bean abstract, so you cannot creat object for parent bean.
<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-3.0.xsd">
<bean id="baseEmployeeBean" class="com.java2novice.beans.Employee" abstract="true">
<property name="company" value="Java2novice" />
</bean>
<bean id="myEmployeeBean" parent="baseEmployeeBean">
<property name="empId" value="1016" />
<property name="name" value="Nataraja Gootooru" />
</bean>
</beans>
|
Inheritance Template
You can create a inheritance template which can be inherited by other child bean definitions. While defining a inheritance template, you should not specify
class attribute and should specify abstract attribute as true. Here is the example configuration:
<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-3.0.xsd">
<bean id="baseEmployeeBean" abstract="true">
<property name="company" value="Java2novice" />
</bean>
<bean id="myEmployeeBean" parent="baseEmployeeBean"
class="com.java2novice.beans.Employee">
<property name="empId" value="1016" />
<property name="name" value="Nataraja Gootooru" />
</bean>
</beans>
|
|