Home > Spring > Spring Application Context example

Spring Application Context example

In this tutorial, we are going to learn about the ApplicationContext interface. We will also find the difference between  BeanFactory interface and ApplicationContext interface.

What is ApplicationContext?

ApplicationContext is an advanced version of BeanFactory and it also has some great feature which helps us while developing any Spring application.

ApplicationContext is the central interface of the application. It is read-only at runtime, but can be reloaded if needed and is compatible with the application. Several classes implement the ApplicationContext interface, which allows a variety of configuration options and types of applications.

ApplicationContext provides the following features:

  • Bean Factory methods to access the components of the application.
  • The ability to load file resources generically.
  • The ability to publish events for registered earphones.
  • The ability to resolve messages to support internationalization.
  • inheritance of the main context.

Difference between  BeanFactory interface and ApplicationContext interface.

BeanFactory uses lazy initialization approachApplicationContext uses early initialization approach
BeanFactory creates a singleton bean only when it is requested from itApplicationContext creates all singleton beans at the time of its own initialization
Not supports internationalizationsupports internationalization

Difference between the implementation of BeanFactory and ApplicationContext


//Find xml file
Resource resource=new ClassPathResource("resources/spring.xml");
//Load xml into container
BeanFactory factory =new XmlBeanFactory(resource);
//Create Object of Test Class with reference of "t"
Object o=factory.getBean("h");
Hello he=(Hello)o;


//Find xml file and load xml file into container as well
ApplicationContext ap=new ClassPathXmlApplicationContext("resources/spring.xml");
Hello he=(Hello)ap.getBean("h");

ApplicationContext example:

We are using Ecplise IDE and some required jars to develop below application.

ApplicationContext in Spring

Ecplise IDE : Ecplise Luna

Jars: Placed below jar in “Referenced Libraries” Folder

  1. commons-logging-1.2
  2. spring-beans-5.0.8.RELEASE
  3. spring-context-5.0.8.RELEASE
  4. spring-core-5.0.8.RELEASE

To create this example, we have created 3 files inside src directory

  1. Hello.java
  2. spring.xml
  3. client.java


package beans;
public class Hello
  private Hello() //Accept private constructor 
    System.out.println("Hello Constructor");
 public void hi()
   System.out.println("Hi All!");



   <bean id="h" class="beans.Hello" scope="prototype"/>



package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.Hello;

public class Client 
  public static void main(String[] args) 
     ApplicationContext ap=new ClassPathXmlApplicationContext("resources/spring.xml");
     Hello he=(Hello)ap.getBean("h");


Hello Constructor
Hi All!

Related Topics:-

You may also like :-

We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

Your email address will not be published. Required fields are marked *