Thursday, April 3, 2014

Spring Tutorial 04 - Write Spring Program Using ApplicationContext

Before starting program let's get into details of the 'ApplicationContext'.

What is ApplicationContext?

ApplicationContext is an interface for providing configuration information to an application. There are multiple classes provided by springframework that implements this interface and helps us use configuration information in applications.

The lowest level implementation of the IoC container is the BeanFactory, but it is recommended to use an ApplicationContext for your application. The ApplicationContext is a subclass of the BeanFactory interface so it has all the functionality a BeanFactory has and more. Unless you are writing an application that needs an extremely small memory footprint, BeanFactory shouldn't be used directly.




An ApplicationContext provides:


  • ApplicationContext provides all the method of the beanfactory, because it extends BeanFactory.
  • The ability to load file resources in a generic fashion. Inherited from the ResourceLoader interface.
  • The ability to publish events to registered listeners. Inherited from the ApplicationEventPublisher interface.
  • The ability to resolve messages, supporting internationalization. Inherited from the MessageSource interface.
  • Inheritance from a parent context. Definitions in a descendant context will always take priority. 

This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet.

public interface ApplicationContext
extends ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver

Above interface is used in Spring.

The most commonly used ApplicationContext implementations are:

FileSystemXmlApplicationContext: This container loads the definitions of the beans from an XML file. Here you need to provide the full path of the XML bean configuration file to the constructor.

ClassPathXmlApplicationContext: This container loads the definitions of the beans from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH.

WebXmlApplicationContext: This container loads the XML file with definitions of all beans from within a web application.



Let's create a program to understand the concept of ApplicationContext.

Create a java file with the name of 'Area' inside the package 'org.javaIsEasy.springApplicationContextExample' and copy below code.

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

public class Area {

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

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

Create a java file with the name of 'CallAreaApplication' inside the package 'org.javaIsEasy.springApplicationContextExample' and copy below code.

CallAreaApplication.java

*******************************************************************************
package org.javaIsEasy.springApplicationContextExample;

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

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

Create a xml file with the name of 'Beans.xml' inside the package 'org.javaIsEasy.springApplicationContextExample' and copy below code.

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.springApplicationContextExample.Area">
      
   </bean>

</beans>

*******************************************************************************
Structure of the project.




Now let's run this program and what results comes. Open 'CallAreaApplication.java' file---> right click---> run as java application.

OUTPUT:
*******************************************************************************
Area Has been calculated
*******************************************************************************

No comments:

Post a Comment