Inheritance Example in Spring Without Java Super Class
Friday, June 11, 2010
In the last example we saw how parent child relationship (inheritance) can be configured in Spring. In this example we will have a look wherein the parent class will not exist in byte code. This will be represented by the configuration option in Spring.
1. Our example scenario is same as previous scenario. We will see how we can create a bean in the container without explicitly having the java class. The process is simple. While declaring the bean mention it as abstract. Here is the relevant portion in the spring config file.
2. Make the following changes in the Child class.
package headfirstspring;
public class Child {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void showFullName(){
System.out.println(" full="" name="" is="">
}
}
3. Here is the program to test it. TestParentChild.java
Read more...
package headfirstspring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestParentChild {
public static void main(String args[]){
// create and configure beans
ApplicationContext context =
new ClassPathXmlApplicationContext(" xml="">
// retrieve configured instance
Child child = (Child)context.getBean("child");
child.showFullName();
}
}