Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


Bean Inheritance

If there are a lot of bean definition into our application and few of the bean are dependent or uses the same properties that are being defined repeatedly or need to be defined again and again. In order to save this configuration and code space, there is better way to use the inheritance functionality of the Object Oriented Paradigm. Using inheritance, we can inherit any pre-defined bean into another bean definition. In this way, we can have a separate bean that may contain all these common values and this bean can be inherited by other beans. So, Parent bean will be inherited by the child bean definition.

A child bean or inherited bean can inherit its parent bean configurations, properties and some attributes. In additional, the child beans are allow to override the inherited value.

See following full example to show you how bean configuration inheritance works in Spring.

Employee.java

package inheritance;

public class Employee {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this .name = name;
}
}


Address.java
package inheritance;

public class Address {

String city;
String state;
String name;

public String getName() {
return name;
}

public void setName(String name) {
this .name = name;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this .city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this .state = state;
}

public String toString() {
return "Name:" + name + " City:" + city + " State:" + state;
}
}

context.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">

<bean id="employee" class="inheritance.Employee">
<property name="name" value="Test"/>
</bean>

<bean id="address" class="inheritance.Address" parent="employee">
<property name="city" value="Gurgaon"/>
<property name="state" value="Haryana"/>
</bean>


</beans>

Main.java
package inheritance;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.
ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "inheritance/context.xml");
Address address = context.getBean("address", Address.class);
System.out.println("name :: "+address.getName());
System.out.println("city :: "+address.getCity());
System.out.println("state :: "+address.getState());
}
}


     << Previous
Next >>