Tuesday, November 4, 2014

Spring Tutorial 12 – Use Collections In Spring

Spring also provides to use collections framework, values can be injected into collections type (List, Set, Map, and Properties). Spring supports majorly four types of collections.
All collections which spring uses have same definition as defined in java.

  • List <list>: A list of values can be injected while having the duplicates.
  • Set <set>: A list of values can be injected while having the no duplicate elements.
  • Map <map>: In this collection a key-value pair elements are injected, which can be of any types.
  • Property <props>: A name-values pair collection are injected in this property tag, this tag has condition that both name and value must be of String type.
In this tutorial we will see that how list collection holds the reference of beans. List collection can be used to hold the values as well.
Let’s get into this detail with following block of code.
Let’s create a project say “SpringTutorial” now add the required jar support to spring, to add the jar you can follow the Spring Tutorial 02 -My First 'Hello World' Program in Spring blog.
Now create a package with the name of “org.javaIsEasy.springCollectionExample“.
Now create a java file with the name of “Rectangle.java” in this package.

Rectangle.java
*******************************************************************************
package org.javaIsEasy.springCollectionExample;
public class Rectangle {
            private int length;
            private int width;
            public int getLength() {
                        return length;
            }
            public void setLength(int length) {
                        this.length = length;
            }
            public int getWidth() {
                        return width;
            }
            public void setWidth(int width) {
                        this.width = width;
            }
}
*******************************************************************************
Create another java file in the same package with the name of “Area.java”.
In the Area file we will be creating object of the Rectangle class, we will give the reference of the Rectangle class in this Area class. So let’s see how it goes.
Area.java

*******************************************************************************
package org.javaIsEasy.springCollectionExample;
import java.util.List;
public class Area {
            private List<Rectangle> rectangle;
            public List<Rectangle> getRectangle() {
                        return rectangle;
            }
            public void setRectangle(List<Rectangle> rectangle) {
                        this.rectangle = rectangle;
            }
           
}
*******************************************************************************

Now create another java file with the name of “CallAreaApplication.java” in the same package.
This class has the main method; configuration file will be loaded in this class and let’s see how bean is called here.
CallAreaApplication.java

*******************************************************************************
package org.javaIsEasy.springCollectionExample;
import java.util.ArrayList;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CallAreaApplication {
public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("org/javaIsEasy/springCollectionExample/Beans.xml");
            Area area=(Area)context.getBean("AreaBean");
            ArrayList<Rectangle> rectangleDataList=(ArrayList<Rectangle>) area.getRectangle();
                        System.out.println("Small Area ------> Length : "+rectangleDataList.get(0).getLength()+" And Width : "+rectangleDataList.get(0).getWidth());
                        System.out.println("Medium Area ------> Length : "+rectangleDataList.get(1).getLength()+" And Width : "+rectangleDataList.get(1).getWidth());
                        System.out.println("Big Area ------> Length : "+rectangleDataList.get(2).getLength()+" And Width : "+rectangleDataList.get(2).getWidth());
                        }
}
*******************************************************************************

Now we have to create bean configuration file with the name of “Beans.xml” in the same package.

Here the Beans.xml file is used to define spring bean configuration. The following code shows how to create inner beans.

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="AreaBean" class="org.javaIsEasy.springCollectionExample.Area">
      <property name="rectangle">
      <list>
            <ref bean="smallArea"/>
            <ref bean="mediumArea"/>
            <ref bean="bigArea"/>
      </list>
     
      </property>
     </bean>

            <bean id="smallArea" class="org.javaIsEasy.springCollectionExample.Rectangle">
      <property name="length" value="3"/>
      <property name="width" value="2"/>
   </bean>
  
   <bean id="mediumArea" class="org.javaIsEasy.springCollectionExample.Rectangle">
      <property name="length" value="6"/>
      <property name="width" value="4"/>
   </bean>
  
   <bean id="bigArea" class="org.javaIsEasy.springCollectionExample.Rectangle">
      <property name="length" value="8"/>
      <property name="width" value="6"/>
   </bean>
  
</beans>
*******************************************************************************

Structure of the project :

 Spring_Tutorial_11_01

Let’s run this program to get the result:

Output:

*******************************************************************************
smallArea length–>3, width–>2
mediumArea length–>6, width–>4
bigArea length–>8, width–>6


*******************************************************************************

Spring Tutorial 11 – Use of alias and name in Beans In Spring

Alias is used when we need to have one name of a particular bean for the different use. Let’s take a scenario that a project has multiple module and we want to have same name of the bean for the different module. Let’s say a project has module A, B and C. The alias tag is used in bean when project needs loose coupling. In this scenario when A module is loaded with a particular name of alias of the bean, other B module and C module can also refer same alias name of the bean when they are loaded. Benefit of this is that we do not need to change the bean name with specific module. Same alias name is being used for all modules.
            If we want to change the name of the alias then we have to change only in one place and same name will be reflected for other modules as well just because of the loose coupling which spring provides.
Overall concept to use this is that if you want to use a particular name for a different purpose use alias.
Let’s get into alias details with following block of code.
Let’s create a project say “SpringTutorial” now add the required jar support to spring, to add the jar you can follow the Spring Tutorial 02 -My First 'Hello World' Program in Spring blog.

Now create a package with the name of “org.javaIsEasy.springInnerBeanAliasExample “.

Now create a java file with the name of “Rectangle.java” in this package.

Rectangle.java

*******************************************************************************
[sourcecode language="java"]
package org.javaIsEasy.springInnerBeanAliasExample;

public class Rectangle {

            private int length;
            private int width;
           
            public int getLength() {
                        return length;
            }
            public void setLength(int length) {
                        this.length = length;
            }
            public int getWidth() {
                        return width;
            }
            public void setWidth(int width) {
                        this.width = width;
            }
           
           
}
[/sourcecode]
*******************************************************************************

Create another java file in the same package with the name of “Area.java”.

In the Area file we will be creating object of the Rectangle class, we will give the reference of the Rectangle class in this Area class. So let’s see how it goes.

Area.java

*******************************************************************************
package org.javaIsEasy.springInnerBeanAliasExample;

public class Area {

            private Rectangle smallArea;
            private Rectangle mediumArea;
            private Rectangle bigArea;
           
           
            public Rectangle getSmallArea() {
                        return smallArea;
            }

            public void setSmallArea(Rectangle smallArea) {
                        this.smallArea = smallArea;
            }
            public Rectangle getMediumArea() {
                        return mediumArea;
            }

            public void setMediumArea(Rectangle mediumArea) {
                        this.mediumArea = mediumArea;
            }
            public Rectangle getBigArea() {
                        return bigArea;
            }

            public void setBigArea(Rectangle bigArea) {
                        this.bigArea = bigArea;
            }
           


            public void getAreaParameter()
            {
                        System.out.println("smallArea length-->"+getSmallArea().getLength()+", width-->"+getSmallArea().getWidth());
                        System.out.println("mediumArea length-->"+getMediumArea().getLength()+", width-->"+getMediumArea().getWidth());
                        System.out.println("bigArea length-->"+getBigArea().getLength()+", width-->"+getBigArea().getWidth());
            }
           
           
}
*******************************************************************************

Now create another java file with the name of “CallAreaApplication.java” in the same package.

This class has the main method; configuration file will be loaded in this class and let’s see how bean is called here.

CallAreaApplication.java

*******************************************************************************
package org.javaIsEasy.springInnerBeanAliasExample;

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

public class CallAreaApplication {
public static void main(String[] args) {
           
            ApplicationContext context = new ClassPathXmlApplicationContext("org/javaIsEasy/springInnerBeanAliasExample/Beans.xml");
           
            Area area=(Area)context.getBean("areaBean-alias");
            area.getAreaParameter();
           
}
}
*******************************************************************************

Now we have to create bean configuration file with the name of “Beans.xml” in the same package.

Here the Beans.xml file is used to define spring bean configuration. The following code shows how to create inner beans.

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="AreaBean" class="org.javaIsEasy.springInnerBeanAliasExample.Area">
                        <property name="smallArea">
                                    <bean class="org.javaIsEasy.springInnerBeanAliasExample.Rectangle">
                                                <property name="length" value="3" />
                                                <property name="width" value="2" />
                                    </bean>
                        </property>
                        <property name="mediumArea">
                                    <bean class="org.javaIsEasy.springInnerBeanAliasExample.Rectangle">
                                                <property name="length" value="6" />
                                                <property name="width" value="4" />
                                    </bean>
                        </property>
                        <property name="bigArea">

                                    <bean class="org.javaIsEasy.springInnerBeanAliasExample.Rectangle">
                                                <property name="length" value="8" />
                                                <property name="width" value="6" />
                                    </bean>
                        </property>
            </bean>
<alias name="AreaBean" alias="areaBean-alias"/>
</beans>
*******************************************************************************

Structure of the project :

 
Let’s run this program to get the result:

Output:

*******************************************************************************

smallArea length–>3, width–>2

mediumArea length–>6, width–>4

bigArea length–>8, width–>6

*******************************************************************************
If you want to use name instead of alias you have change in the Beans.xml and in same way where it needs to be called also need changes.
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="AreaBean" class="org.javaIsEasy.springInnerBeanAliasExample.Area" name="areaNameBean">
             
                        <property name="smallArea">
                                    <bean class="org.javaIsEasy.springInnerBeanAliasExample.Rectangle">
                                                <property name="length" value="3" />
                                                <property name="width" value="2" />
                                    </bean>
                        </property>
                        <property name="mediumArea">
                                    <bean class="org.javaIsEasy.springInnerBeanAliasExample.Rectangle">
                                                <property name="length" value="6" />
                                                <property name="width" value="4" />
                                    </bean>
                        </property>
                        <property name="bigArea">

                                    <bean class="org.javaIsEasy.springInnerBeanAliasExample.Rectangle">
                                                <property name="length" value="8" />
                                                <property name="width" value="6" />
                                    </bean>
                        </property>
            </bean>

</beans>

*******************************************************************************
CallAreaApplication.java class also needs to be changed to call bean while having the name rather than id as mentioned below.

CallAreaApplication.java

*******************************************************************************
package org.javaIsEasy.springInnerBeanAliasExample;

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

public class CallAreaApplication {
public static void main(String[] args) {
           
            ApplicationContext context = new ClassPathXmlApplicationContext("org/javaIsEasy/springInnerBeanAliasExample/Beans.xml");
           
            Area area=(Area)context.getBean("areaNameBean");
            area.getAreaParameter();
           
}
}
*******************************************************************************

Spring Tutorial 10 – How To Use Inner Beans In Spring

As we have seen in earlier tutorial that we have created separate beans and whenever those bean required by other object, beans can be called by the id and ref pair tag. If there is no requirement of creating separate beans in the project then inner beans can be used which spring provides. We will see in this blogs that rather than giving id or reference tag for the separate bean, we can write the beans inside of a particular class and those beans cannot be used outside of the object. So this is one feature, spring provides.
            Let’s get into this tutorial by the following example of code.
Let’s create a project say “SpringTutorial” now add the required jar support to spring, to add the jar you can follow the Spring Tutorial 02 -My First 'Hello World' Program in Spring blog.

Now create a package with the name of “org.javaIsEasy.springInnerBeanAliasExample “.

Now create a java file with the name of “Rectangle.java” in this package.
Rectangle.java

*******************************************************************************
package org.javaIsEasy.springInnerBeanAliasExample;

public class Rectangle {

            private int length;
            private int width;
           
            public int getLength() {
                        return length;
            }
            public void setLength(int length) {
                        this.length = length;
            }
            public int getWidth() {
                        return width;
            }
            public void setWidth(int width) {
                        this.width = width;
            }
           
           
}
*******************************************************************************
Create another java file in the same package with the name of “Area.java”.

In the Area file we will be creating object of the Rectangle class and another parameter with the name of ‘areaType’, we will give the reference of the Rectangle class in this Area class. So let’s see how it goes.
Area.java

*******************************************************************************
package org.javaIsEasy.springInnerBeanAliasExample;

public class Area {

            private Rectangle smallArea;
            private Rectangle mediumArea;
            private Rectangle bigArea;
           
           
            public Rectangle getSmallArea() {
                        return smallArea;
            }

            public void setSmallArea(Rectangle smallArea) {
                        this.smallArea = smallArea;
            }
            public Rectangle getMediumArea() {
                        return mediumArea;
            }

            public void setMediumArea(Rectangle mediumArea) {
                        this.mediumArea = mediumArea;
            }
            public Rectangle getBigArea() {
                        return bigArea;
            }

            public void setBigArea(Rectangle bigArea) {
                        this.bigArea = bigArea;
            }
           


            public void getAreaParameter()
            {
                        System.out.println("smallArea length-->"+getSmallArea().getLength()+", width-->"+getSmallArea().getWidth());
                        System.out.println("mediumArea length-->"+getMediumArea().getLength()+", width-->"+getMediumArea().getWidth());
                        System.out.println("bigArea length-->"+getBigArea().getLength()+", width-->"+getBigArea().getWidth());
            }
           
           
}
*******************************************************************************

Now create another java file with the name of “CallAreaApplication.java” in the same package.

This class has the main method; configuration file will be loaded in this class and let’s see how bean is called here.

CallAreaApplication.java

*******************************************************************************
package org.javaIsEasy.springInnerBeanAliasExample;

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

public class CallAreaApplication {
public static void main(String[] args) {
           
            ApplicationContext context = new ClassPathXmlApplicationContext("org/javaIsEasy/springInnerBeanAliasExample/Beans.xml");
           
            Area area=(Area)context.getBean("AreaBean");
            area.getAreaParameter();
           
}
}
*******************************************************************************
Now we have to create bean configuration file with the name of “Beans.xml” in the same package.

Here the Beans.xml file is used to define spring bean configuration. The following code shows how to set a property value through constructor injection.

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="AreaBean" class="org.javaIsEasy.springInnerBeanAliasExample.Area">
                        <property name="smallArea">
                                    <bean class="org.javaIsEasy.springInnerBeanAliasExample.Rectangle">
                                                <property name="length" value="3" />
                                                <property name="width" value="2" />
                                    </bean>
                        </property>
                        <property name="mediumArea">
                                    <bean class="org.javaIsEasy.springInnerBeanAliasExample.Rectangle">
                                                <property name="length" value="6" />
                                                <property name="width" value="4" />
                                    </bean>
                        </property>
                        <property name="bigArea">

                                    <bean class="org.javaIsEasy.springInnerBeanAliasExample.Rectangle">
                                                <property name="length" value="8" />
                                                <property name="width" value="6" />
                                    </bean>
                        </property>
            </bean>

</beans>
*******************************************************************************
Structure of the project :
Let’s run this program to get the result:

Output:

*******************************************************************************
smallArea length-->3, width-->2
mediumArea length-->6, width-->4
bigArea length-->8, width-->6

*******************************************************************************

Spring tutorial 09 - Injecting Multiple Objects in Spring

We have already got some information regarding injection (by constructor and setter). In this tutorial we will get to know that how multiple bean objects at the run time.

Let's understand this with an example.

First create a project say "SpringTutorial" now add the required jar support to spring, to add the jar you can follow the Spring Tutorial 02 -My First 'Hello World' Program in Spring blog.

Now create a package with the name of "org.javaIsEasy.springInjectionObjectsExample".

Now create a java file with the name of "Rectangle.java" in this package.

Rectangle.java

*******************************************************************************

package org.javaIsEasy.springInjectionObjectsExample;

public class Rectangle {

 private int length;
 private int width;

 public int getLength() {
  return length;
 }
 public void setLength(int length) {
  this.length = length;
 }
 public int getWidth() {
  return width;
 }
 public void setWidth(int width) {
  this.width = width;
 }


}

*******************************************************************************
Create an other java file in the same package with the name of "Area.java".

In the Area file we will creating object of the Rectangle class, we will give the reference of the Rectangle class in this Area class. So let's see how it goes.

Area.java
*******************************************************************************
package org.javaIsEasy.springInjectionObjectsExample;

public class Area {

 private Rectangle smallArea;
 private Rectangle mediumArea;
 private Rectangle bigArea;





 public Rectangle getSmallArea() {
  return smallArea;
 }

 public void setSmallArea(Rectangle smallArea) {
  this.smallArea = smallArea;
 }
 public Rectangle getMediumArea() {
  return mediumArea;
 }

 public void setMediumArea(Rectangle mediumArea) {
  this.mediumArea = mediumArea;
 }
 public Rectangle getBigArea() {
  return bigArea;
 }

 public void setBigArea(Rectangle bigArea) {
  this.bigArea = bigArea;
 }


 public void getAreaParameter()
 {
  System.out.println("smallArea length-->"+getSmallArea().getLength()+", width-->"+getSmallArea().getWidth());
  System.out.println("mediumArea length-->"+getMediumArea().getLength()+", width-->"+getMediumArea().getWidth());
  System.out.println("bigArea length-->"+getBigArea().getLength()+", width-->"+getBigArea().getWidth());
 }


}

*******************************************************************************

Now we have to create bean configuration file with the name of "Beans.xml" in the same package. 

Here the Beans.xml file is used to define spring bean configuration. The following code shows how to set a property value through constructor injection.

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="AreaBean" class="org.javaIsEasy.springInjectionObjectsExample.Area">
      <property name="smallArea" ref="smallArea"/>
      <property name="mediumArea" ref="mediumArea"/>
      <property name="bigArea" ref="bigArea"/>
     
   </bean>

 <bean id="smallArea" class="org.javaIsEasy.springInjectionObjectsExample.Rectangle">
      <property name="length" value="3"/>
      <property name="width" value="2"/>
   </bean>
   
   <bean id="mediumArea" class="org.javaIsEasy.springInjectionObjectsExample.Rectangle">
      <property name="length" value="6"/>
      <property name="width" value="4"/>
   </bean>
   
   <bean id="bigArea" class="org.javaIsEasy.springInjectionObjectsExample.Rectangle">
      <property name="length" value="8"/>
      <property name="width" value="6"/>
   </bean>
   
</beans>

*******************************************************************************

Now to call this we have to create another class which will have main method.

Now create another java file with the name of "CallAreaApplication.java" in the same package.
This class has the main method, configuration file will be loaded in this class and let's see how bean is called here.

CallAreaApplication.java
*******************************************************************************
package org.javaIsEasy.springInjectionObjectsExample;

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

public class CallAreaApplication {
public static void main(String[] args) {

 ApplicationContext context = new ClassPathXmlApplicationContext("org/javaIsEasy/springInjectionObjectsExample/Beans.xml");

 Area area=(Area)context.getBean("AreaBean");
 area.getAreaParameter();

 }
}

*******************************************************************************

Structure of the project :




Let's run this program to get the result:

Output:

*******************************************************************************
smallArea length-->3, width-->2
mediumArea length-->6, width-->4
bigArea length-->8, width-->6

*******************************************************************************