Wednesday, June 26, 2013

Spring Collections Using Util XML tags.

    In this post I am going to explain about Spring Util tags, how to use. Spring frameworks provides Util schema to configure Collections in spring bean configuration file. The util tags deal with common, utility configuration issues, such as configuring collections.
To use the tags in the util schema, you need to have the following preamble at the top of your Spring XML configuration file (bold ones).

you can also refer my previous post to learn SpringCore and SpringAutowire

<?xml version="1.0" encoding="UTF-8"?>
<beans
      xmlns="http://www.springframework.org/schema/beans"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-3.0.xsd">

      <!—Your bean configurations -->
</bean>

Here Is my spring bean configuration file. Here I have configured 4 collection beans with the ids list, set, map and properties.

list:  will hold the list of items, internally spring container will create java.util.ArrayList object and set the values into indexes.

set: will hold the set of items, internally spring container will create java.util.LinkedHashSet object and set the values into indexes.

            Both List and Set have <value>…</value> as child element in it’s to pass the values.

map: will hold the key-value pairs so we have to provide both key and values in your spring configuration file using the child element <entry key=”” value=”” />. Internally spring container will create java.util.LinkedHashMap object with the specified key value pairs.

properties: will hold the key = value pairs so we have to provide key value pairs in spring configuration file using the child element <prop key=”” >…</prop>. Internally spring container will create java.util.Properties object with the specified key=value pairs.


Here I configured one more bean with the name “sampleCollection” and I am using autowireby Name to auto inject all the above collection beans into my SampleCollection class.

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"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
                                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                                    http://www.springframework.org/schema/util
                                    http://www.springframework.org/schema/util/spring-util-3.0.xsd">
      <util:list id="list">
             <value>listItem1</value>
             <value>listItem2</value>
             <value>listItem3</value>
      </util:list>
     
      <util:set id="set">
            <value>setItem1</value>
            <value>setItem2</value>
            <value>setItem3</value>
      </util:set>
     
      <util:map id="map">
            <entry key="mapKey1" value="mapValue1" />
            <entry key="mapKey2" value="mapValue2" />
            <entry key="mapKey3" value="mapValue3" />
      </util:map>
       
      <util:properties id="properties" scope="singleton">
            <prop key="propKey1">
                  PropValue1
            </prop>
            <prop key="propKey2">
                  PropValue2
            </prop>
      </util:properties>
     
      <bean id="sampleCollection" class="com.lova.spring.util.SampleCollection" autowire="byName" />
     
      <!-- or injecting through xml configuration-->
      <!--
      <bean id="sampleCollection1" class="com.lova.spring.util.SampleCollection">
                  <property name="list" ref="list" />
                  <property name="set" ref="set" />
                  <property name="map" ref="map" />
                  <property name="properties" ref="properties" />
    </bean>
    -->
     
</beans>

SampleCollection.java

package com.lova.spring.util;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @author Lovababu
 *
 */
public class SampleCollection
{

      private List<String> list;
     
      private Set<String> set;
     
      private Map<String, String> map;
     
      private Properties properties;

      // Generate setter and getter method before run the test app.
     
}

SampleCollectionTest.java

package com.lova.spring.util.test;

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

import com.lova.spring.util.SampleCollection;

/**
 * @author Lovababu
 *
 */
public class SampleCollectionTest
{

      public static void main(String[] args)
{
           
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
           
            SampleCollection sampleCollection = (SampleCollection) context.getBean("sampleCollection");
             
            System.out.println("************List Items************");
            System.out.println(sampleCollection.getList());
            System.out.println();
           
            System.out.println("*************SetItems**************");
            System.out.println(sampleCollection.getSet());
            System.out.println();
           
            System.out.println("************Map Items**************");
            System.out.println(sampleCollection.getMap());
            System.out.println();
           
            System.out.println("********PropertiesItems*************");
            System.out.println(sampleCollection.getProperties());
            System.out.println();
      }

}

Required Libraries:

Ø      org.springframework.core-3.0.5.RELEASE.jar
Ø      org.springframework.context-3.0.5.RELEASE.jar
Ø      org.springframework.beans-3.0.5.RELEASE.jar
Ø      org.springframework.asm-3.0.5.RELEASE.jar
Ø      org.apache.log4j-1.2.15.jar
Ø      org.apache.commons.logging-1.1.1.jar
Ø      org.springframework.expression-3.0.5.RELEASE.jar

Thank you please leave you comment. :)

Monday, June 24, 2013

Spring Bean inheritance for beginner

Inheritance:  Inheritance is a way of establishing IS A relationship.
When you want to create a new a class “B” and there is already one class “A” has existed that is having some method and properties those you want to implement in your class, then you can derive your class from the existing class so that you can reuse the functionality.

For Spring Intruduction and Spring Basic Example visit my previous posts below

Spring Intruduction  Spring Core

Definitions: The class that is derived from the class is called sub class (derived class or Child class or extended class). The class from which your class is derived is called super class (base class or parent class).

From the above definition class B is sub class and A is super class.




Let’s have a look at my spring bean configuration file.

Here I have configured my super class “Employee” with the id employee , to this bean I am passing only common value like company name and location because all employees working under my company having same values for these two properties.

Configured one more bean “Manager” with the id manager, I want make it sub class for my super class Employee to do this I added one attribute in my bean configuration i.e. parent=”employee”, this will inform to the Spring container this class is derived from the Employee super class so that I can reuse the properties and method from the Employee class instead of writing again and again (Boiler plat code).


Configured one more bean “Developer” with the Id developer, same configuration applicable as manager bean.

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"
      xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

      <bean id="employee" class="com.lova.inheritance.Employee">
            <property name="company" value="TP" />
            <property name="location" value="Bangalore"/>
      </bean>
     
      <bean id="manager" class="com.lova.inheritance.Manager" parent="employee">
            <property name="empName" value="Durga" />
            <property name="empNo" value="1310" />
            <property name="salary" value="20000" />
      </bean>
      <bean id="developer" class="com.lova.inheritance.Developer" parent="employee">
            <property name="empName" value="Lova" />
            <property name="empNo" value="1311" />
            <property name="salary" value="15000" />
      </bean>
</beans>


Employee.java

package com.lova.inheritance;

/**
 * @author Lovababu
 *
 */
public class Employee {

      private String empNo;

      private String empName;

      private double salary;
     
      private String company;

      private String location;
     
      //setter and getter method, you can generate using IDE.

      public void displayEmpInfo()
      {
            System.out.println("[Employee: Name:" + empName + " ID:" +  empNo
                        + " Company:" + company + " Location:" + location);
      }
}


Manager.java

I haven’t defined any properties in this class, but still I am able to set some data in this Object using inheritance.  All properties and diplayEmpInfo() method will be inherited from the super class Employee. For manager my company is offering 15% hike calculateHike () method will calculate managers hike it will display the accumulated salary.

package com.lova.inheritance;


/**
 * @author Lovababu
 *
 */
public class Manager extends Employee{

      public void calculateHike()
      {
            double hike = getSalary()*(00.15);
            System.out.println("Offer Amount will be:"+(getSalary()+hike));
      }
}

Developer.java

For Developer my company is offering 10% hike calculateHike () method will calculate managers hike it will display the accumulated salary.

package com.lova.inheritance;

/**
 * @author Lovababu
 *
 */
public class Developer extends Employee
{
      public void calculateHike()
      {
            double hike = getSalary()*(00.10);
            System.out.println("Offer Amount will be:"+(getSalary()+hike));
      }
}

SpringBeanInheritanceTest.java

This test program gets the manager bean and developer bean from the context and invokes the corresponding classes method to calculate hike and super class method to display employee info.


package com.lova.inheritance.test;

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

import com.lova.inheritance.Developer;
import com.lova.inheritance.Manager;

/**
 * @author Lovababu
 *
 */
public class SpringBeanInheritanceTest {

      public static void main(String[] args)
      {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
           
            //working with manager
            Manager manager = (Manager) context.getBean("manager");
            manager.displayEmpInfo();
            manager.calculateHike();
            //working with developer
            System.out.println("**************************************************");
            Developer developer = (Developer) context.getBean("developer");
            developer.displayEmpInfo();
            developer.calculateHike();
           
      }
}


Required Libraries:

Ø       org.springframework.core-3.0.5.RELEASE.jar
Ø       org.springframework.context-3.0.5.RELEASE.jar
Ø       org.springframework.beans-3.0.5.RELEASE.jar
Ø       org.springframework.asm-3.0.5.RELEASE.jar
Ø       org.apache.log4j-1.2.15.jar
Ø       org.apache.commons.logging-1.1.1.jar
Ø       org.springframework.expression-3.0.5.RELEASE.jar
UA-41474183-1