The largest Interview Solution Library on the web


« Previous | 2 | | 4 | Next »

Spring - Bean Definition Inheritance

  • 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.
  •   
  • Spring Bean definition inheritance has nothing to do with Java class inheritance but inheritance concept is same.
  •   
  • You can define a parent bean definition as a template and other child beans can inherit required configuration from the parent bean.
  •   
  • 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.
  •     

    Spring - Bean Definition Inheritance Example

    HelloWorld.java

    package com.jtc;
    public class HelloWorld {
    private String message1;
    private String message2;
    public void setMessage1(String message){
    this.message1 = message;
    }
    public void setMessage2(String message){
    this.message2 = message;
    }
    public void getMessage1(){
    System.out.println("World Message1 : " + message1);
    }
    public void getMessage2(){
    System.out.println("World Message2 : " + message2);
    }
    }

    InitHelloWorld.java

    package com.jtc;
    public class InitHelloWorld {
    private String message1;
    private String message2;
    private String message3;
    public void setMessage1(String message){
    this.message1 = message;
    }
    public void setMessage2(String message){
    this.message2 = message;
    }
    public void setMessage3(String message){
    this.message3 = message;
    }
    public void getMessage1(){
    System.out.println("India Message1 : " + message1);
    }
    public void getMessage2(){
    System.out.println("India Message2 : " + message2);
    }
    public void getMessage3(){
    System.out.println("India Message3 : " + message3);
    }
    }

    MainApp.java

    package com.jtc;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class MainApp {
    public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
    objA.getMessage1();
    objA.getMessage2();
    InitHelloWorld objB = (InitHelloWorld) context.getBean("helloIndia");
    objB.getMessage1();
    objB.getMessage2();
    objB.getMessage3();
    }
    }

    Beans.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-3.0.xsd">
    
       <bean id="helloWorld" class="com.jtc.HelloWorld">
          <property name="message1" value="Hello World!"/>
          <property name="message2" value="Hello Second World!"/>
       </bean>
    
       <bean id="helloIndia" class="com.jtc.HelloIndia" parent="helloWorld">
          <property name="message1" value="Hello India!"/>
          <property name="message3" value="Namaste India!"/>
       </bean>
    
    </beans>
    </beans>

    Add Below Libraries
    commons-logging-1.1.1
    spring-aop-4.1.6.RELEASE
    spring-aspects-4.1.6.RELEASE
    spring-beans-4.1.6.RELEASE
    spring-context-4.1.6.RELEASE
    spring-context-support-4.1.6.RELEASE
    spring-core-4.1.6.RELEASE
    spring-expression-4.1.6.RELEASE
    spring-instrument-4.1.6.RELEASE
    spring-instrument-tomcat-4.1.6.RELEASE
    spring-jdbc-4.1.6.RELEASE
    spring-jms-4.1.6.RELEASE
    spring-messaging-4.1.6.RELEASE
    spring-orm-4.1.6.RELEASE
    spring-oxm-4.1.6.RELEASE
    spring-test-4.1.6.RELEASE
    spring-tx-4.1.6.RELEASE
    spring-web-4.1.6.RELEASE
    spring-webmvc-4.1.6.RELEASE
    spring-webmvc-portlet-4.1.6.RELEASE
    spring-websocket-4.1.6.RELEASE

    Bean Definition Template:

  • You can create a Bean definition template which can be used by other child bean definitions without putting much effort.
  • While defining a Bean Definition Template, you should not specify class attribute and should specify abstract attribute with a value of true as shown below:
  • Beans.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-3.0.xsd">
    
       <bean id="beanTeamplate" abstract="true">
          <property name="message1" value="Hello World!"/>
          <property name="message2" value="Hello Second World!"/>
          <property name="message3" value="Namaste India!"/>
       </bean>
    
       <bean id="helloIndia" class="com.jtc.HelloIndia" parent="beanTeamplate">
          <property name="message1" value="Hello India!"/>
          <property name="message3" value="Namaste India!"/>
       </bean>
    
    </beans>

    OutPut
    World Message1 : Hello World!
    World Message2 : Hello Second World!
    India Message1 : Hello India!
    India Message2 : Hello Second World!
    India Message3 : Namaste India!

    Note:

    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.
    « Previous | 1 | 2 | 3 | Next »


    copyright © 2014 - all rights riserved by javatechnologycenter.com