Friday, 28 March 2014

Using inherit attribute in Spring bean tag

The following example illustrates using inherit attribute in the spring <bean> tag.




1. Create a project in eclipse

  1. File -> New -> Project -> Java Project
  2. Give the project name spring3 and click Finish 
  3. Now, the project is created.
  4. Under the Project Explorer (in the sidebar) you will see spring3. If you aren't able to see Project Explorer, go to Window menu -> Show view -> Project Explorer.
  5. Right click on the spring3 and click Properties
  6. On the left side, click on Java Build Path.
  7. Select the Libraries tab and then click on Add External Jars
  8. Now, add these jar files starting with: spring-beans, spring-context, spring-core, spring-expression, commons-logging
  9. Click on OK and then you are ready.
Now right click on the src folder under spring3 and New -> Class and name it SpringPrg

Main program - SpringPrg.java



package spring3;

import org.springframework.context.support.GenericXmlApplicationContext;

public class SpringPrg {

    @SuppressWarnings("resource")
    public static void main(String args[])
    {
        GenericXmlApplicationContext gc=new GenericXmlApplicationContext();
        gc.load("classpath:applicationContext.xml");
        gc.refresh();
       
        Student st=gc.getBean("st2",Student.class);
       
        System.out.println(st.getSno()+" "+st.getSname()+" "+st.getAge());
    }
   
}

The code here is normal. I have used the GenericXmlApplicationContext class which loads the given xml file and reads the bean definitions i.e. it reads how to create bean class objects from the xml file.

The gc.getBean() method returns the object st2 defined in the XML file. The xml file is given below.

Now, again in the package explorer, right click on spring3 and then New -> Class and name it Student

Bean class - Student.java



package spring3;

public class Student {

private int sno;
private String sname;
private int age;

    public void setSno(int sno)
    {
        this.sno=sno;
    }
   
    public int getSno()
    {
        return sno;
    }
   
    public void setSname(String sname)
    {
        this.sname=sname;
    }
   
    public String getSname()
    {
        return sname;
    }
   
    public void setAge(int age)
    {
        this.age=age;
    }
   
    public int getAge()
    {
        return age;
    }
   
}

This is a very simple bean class that takes three general properties of a Student sno, sname and age. Values to these properties are set in the xml file.

Now right click on the src in package explorer, and New -> Other and type xml file in the textbox, hit enter and give it the name applicationContext.xml.

Bean definitions - applicationContext.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="st1" class="spring3.Student">
            <property name="sno" value="101"/>
            <property name="sname" value="Rama"/>
            <property name="age" value="20"/>
        </bean>
       
        <bean id="st2" class="spring3.Student" parent="st1">
            <property name="age" value="22"/>
        </bean>
       
</beans>

As you can see, first we have created a bean object called st1 for the class spring3.Student and then in it we have set the properties, sno to 101, sname to Rama, age to 20. That code can be interpreted into Java like this..



Student st1=new Student();


st1.setSno(101);


st1.setSname("Rama");


st1.setAge(20);

Now, we have also created another object for Student class called st2. You can notice the parent attribute which points to st1. This means that the properties of st1 will be copied to st2.
Additionally, we have included a property tag in st2 setting age to 22. This line, tells that it overrides the age property copied from the st1. In Java the code can be interpreted as..



Student st2=new Student();


st2.setSno(st1.getSno());


st2.setSname(st1.getSname());


st2.setAge(st1.getAge());


st2.setAge(22);


abstract attribute

In the SpringPrg.java program, you can also get the st1 object like this..

Student st=gc.getBean("st1",Student.class);
System.out.println(st1.getSno()+" "+st1.getSname()+" "+st1.getAge());

But if you don't want st1 to be accessed i.e. looked up or don't want to create an object for it, then you should place the abstract="true" while defining the st1. Replace the line..



<bean id="st1" class="spring3.Student">

with the following..



<bean id="st1" class="spring3.Student" abstract="true">

No comments:

Post a Comment