Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Inheritance in Spring 
 

Inheritance Demo, In the example given below we are going to tell about the inheritance in the Spring framework.

 

Inheritance in Spring

                         

Inheritance Demo, In the example given below we are going to tell about the inheritance in the Spring framework. By inheritance we mean a way of forming new classes using classes that have already been defined. Here we have created a simple bean and used this bean as a template for creating other beans.

<bean id="parent" class="mybean" >:-Creates a  Parent bean which would be used as a template for creating other beans. Here "mybean" is the name of the bean class which would be referred in the xml file with the id "parent".

<bean id="child" class="mybean" parent="parent">:-Creates a child bean which would be inheriting from the parent bean defined above.

parent="parent":-Specify that this bean is inheriting the properties of some other bean. 

<bean id="parent" class="mybean" >         <property name="name" value="Roseindia.net"/>     
</bean>
    <bean id="child" class="mybean" parent="parent">     <property name="address" value="Rohini"/>     
</bean>

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">

    <bean id="parent" class="mybean" >
        <property name="name" value="Roseindia.net"/>
    </bean>

    <bean id="child" class="mybean" parent="parent">
        <property name="address" value="Rohini"/>
    </bean>

    <bean id="subchild" class="mybean" parent="parent"/>

</beans>

Main.java

This is the file through which we are retrieving the property of the bean defined above. Some of the methods which are used here are as follows:-

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml")):-This method creates an instance of the XmlBeanFactory which is used to read bean definition from an XML document

new ClassPathResource("context.xml"):-
Creates a new ClassPathResource for ClassLoader .Here the context.xml is the file which is to be loaded.

(bf.getBean("child")):-This method returns an instance of the bean named "child", which is shared or independent, of the given bean name.

(bf.getBean("subchild")):-This method returns an instance of the bean named "subchild", which is shared or independent, of the given bean name.

stringBuilder.append("Bean"):-This is the method of the class string Builder which appends the specified string to this character sequence.


import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Main {

  public static void main(String[] argsthrows Exception {
    XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("context.xml"));
    System.out.println("===============Inheritance demo=================");
    System.out.println(bf.getBean("child"));
    System.out.println(bf.getBean("subchild"));
    
  }
}
class mybean {
  private String name;
  private String address;

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

    public void setAddress(String address) {
        this.address = address;
    }
 
  @Override
  public String toString() {
      final StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.append("Bean");
      stringBuilder.append("{name='").append(name).append('\'');
      stringBuilder.append(", address=").append(address);
     
      stringBuilder.append('}');
      return stringBuilder.toString();
  }
}

Output of the program

Nov 25, 2008 3:39:29 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [context.xml] ===============Inheritance demo================= Bean{name='Roseindia.net', address=Rohini} Bean{name='Roseindia.net', address=null} BUILD SUCCESSFUL (total time: 1 second)

Download source code

                         
» View all related tutorials
Related Tags: c spring development framework application io enterprise read applications app frame for work ram erp e li dev enter in

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.