Saturday, March 19, 2011

ApplicationContext Interface

An ApplicationContext is much the same as a BeanFactory. Both load bean definitions, wire beans together, and dispense beans upon request. But the ApplicationContext is more powerful container then BeanFactory. It inherits the BeanFactory interface so it provide all the functionality which are there in BeanFactory interface along with the following extra added features:-

  1. Application contexts provide a means for resolving text messages from the properites files, including support for internationalization.
  2. Application contexts provide a generic way to load file resources like images.
  3. Application contexts can publish events to beans that are registered as listeners.

There are various implementation of ApplicationContext is available in Spring world but most commonly used implementation are given below:-

  1. ClassPathXmlApplicationContext—Loads a context definition from an XML file located in the classpath, treating context definition files as classpath resources.
  2. FileSystemXmlApplicationContext—Loads a context definition from an XML file in the file system.
  3. XmlWebApplicationContext—Loads context definitions from an XML file contained within a web application.

Example For ApplicationContext Interface :-

Note:-Lets take the same xml spring-beans.xml file for the ApplicationContext as well and the same TestBean. The following is the ApplicationContext Factory Example file.

ApplicationContextExample.java :

package com.vaani.spring.factory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.vaani.spring.beans.TestBean;

public class ApplicationContextExample {

public static void main (String [] ar){

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"com/vaani/xml/spring-beans.xml"});

TestBean bean = (TestBean)context.getBean("test");

System.out.println(bean.toString());

}

}

No comments:

Post a Comment

Chitika