Thursday, April 3, 2014

Spring tutorial 05- Initializing and injecting value in Bean property

To start this blog, lets understand the Beans.

The beans:
A Spring IoC container manages one or more beans. These beans are created using the configuration metadata that has been supplied to the container (typically in the form of XML <bean/> definitions). Beans are represented as beanDefintion objects which contains following meadata.



  • a package-qualified class name: typically this is the actual implementation class of the bean being defined.
  • bean behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth).
  • references to other beans which are needed for the bean to do its work; these references are also called collaborators or dependencies.
  • other configuration settings to set in the newly created object. An example would be the number of connections to use in a bean that manages a connection pool, or the size limit of the pool.




In Spring, InitializingBean is a marker interface, which is used to initialize the bean.

  • For bean implemented InitializingBean, it will run afterPropertiesSet() after all bean properties have been set
  • For bean implemented DisposableBean, it will run destroy() after Spring container is released the bean.

Example :

In below example we can see, how property can be initialized.

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.springPropertyInitializationExample".
Create a java file in this package with the name of "Area.java".

Area.java
*******************************************************************************

package org.javaIsEasy.springPropertyInitializationExample;

public class Area {

private String areaOf;

 public String getAreaOf() {
 return areaOf;
}

public void setAreaOf(String areaOf) {
 this.areaOf = areaOf;
}

 public void calculateArea()
 {
  System.out.println(getAreaOf()+" Area Has been calculated");
 }
}

*******************************************************************************
Now create another file in the same folder with the name of "CallAreaApplication.java".

CallAreaApplication.java
*******************************************************************************

package org.javaIsEasy.springPropertyInitializationExample;

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/springPropertyInitializationExample/Beans.xml");
 Area area=(Area)context.getBean("area");
 area.calculateArea();
}
}

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

Now at last one more file is need to be created in which we define beans, so let's create "Beans.xml" file in the same folder again.

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="area" class="org.javaIsEasy.springPropertyInitializationExample.Area">
      <property name="areaOf" value="Rectangle"></property>
   </bean>

</beans>

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

in the above xml file we have injecting the value to the "areaOf" object of the Area class, and giving the value of "Rectangle".

Structure of the project :




Lets run this program to get the result:

Output:

*******************************************************************************
Rectangle Area Has been calculated
*******************************************************************************