Followers

springIOC: Inheritance(Super class is Concrete Class)

HelloWorld.java
================
package myspring;

public class HelloWorld {
    private String message1;
    private String message2;

    public void getMessage1() {
        System.out.println("Message1 "+message1);
    }

    public void setMessage1(String message1) {
        this.message1 = message1;
    }

    public void getMessage2() {
        System.out.println("Message2 "+message2);
    }

    public void setMessage2(String message2) {
        this.message2 = message2;
    }
}

HelloIndia.java
================

package myspring;

public class HelloIndia extends HelloWorld{
    private String message1;
    private String message3;

    public void getMessage1() {
        System.out.println("Message1 "+message1);
    }

    public void setMessage1(String message1) {
        this.message1 = message1;
    }

    public void getMessage3() {
        System.out.println("Message3 "+message3);
    }

    public void setMessage3(String message3) {
        this.message3 = message3;
    }
    
    
}

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="myspring.HelloWorld">
      <property name="message1" value="Hello One World"/>
      <property name="message2" value="Hello Second World!"/>
   </bean>

   <bean id="helloIndia" class="myspring.HelloIndia" parent="helloWorld">
      <property name="message1" value="Hello India"/>
      <property name="message3" value="Hello Second India"/>
   </bean>
   

</beans>

Test.java
==========
package myspring;

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

public class Test {

      public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
                    
      HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
      helloWorld.getMessage1();
      helloWorld.getMessage2();

      HelloIndia helloIndia = (HelloIndia) context.getBean("helloIndia");
      helloIndia.getMessage1();   
      helloIndia.getMessage2();
      helloIndia.getMessage3();
          
          
          
    }
    
}

No comments:

Post a Comment