Java Login


Hello guys, is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.

     << Previous
Next >>     

Spring Application Context

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. ApplicationContext provides standard bean factory lifecycle capabilities. An important capability which we will be using in below code example is, class implementing ApplicationContext should scan for ApplicationContextAware beans and invoke setApplicationContext by passing an implementation of its instance.

There are four common implementations of the ApplicationContext interface:

  • ClassPathXmlApplicationContext
  • FileSystemXmlApplicationContext
  • XmlWebApplicationContext
  • AnnotationConfigWebApplicationContext

Difference Between Bean Factory and Application Context

Before seeing difference between ApplicationContext and BeanFactory, we discuss some similarity between both of them. Spring provides two kinds of IOC container, one is BeanFactory and other is ApplicationContext. Both are Java interfaces and ApplicationContext extends BeanFactory. Both of them are configuration using XML configuration file. In short BeanFactory provides basic IOC and DI features while ApplicationContext provides advanced features. Apart from these, Here are few more difference.

  • ApplicationContext is a sub interface of BeanFactory hence ApplicationContext includes all functionalities of BeanFactory
  • BeanFactory provides only the basic features but ApplicationContext provides advanced features like internationalization, event publication, etc
  • ApplicationContext takes more memory than BeanFactory
  • ApplicationContext instantiates the beans at the time of application start up but BeanFactory loads the beans at the time of a getBean() method call
  • ApplicationContext--Support Annotation based dependency Injection.-@Autowired, @PreDestroy but BeanFactory-Does not support the Annotation based dependency Injection
  • ApplicationContext supports more than one configuration files but BeanFactory supports only one configuration file
  • ApplicationContext-Support internationalization (I18N) messages. but BeanFactory-Does not support way to access Message Bundle(internationalization (I18N)
  • Application contexts can publish events to beans that are registered as listeners but bean factory doesn't support

Example of Application Context

To get access to ApplicationContext we should implement ApplicationContextAware interface in the respective java bean. It has a method,

void setApplicationContext(ApplicationContext applicationContext) throws BeansException

The ApplicationContext implementation which we are using in our application will invoke this method and pass the concrete object for AppplicationContext. Using this we can get access to all the configuration information. Let we see one simple example how to access Application Context

package applicationContextAware;

public class Emp {
private String id;
private String name;

public String getId() {
return id;

public void setId(String id) { = id;

public String getName() {
return name;

public void setName(String name) { = name;

public String toString() {
return "Id:: " + id + " Name:: " + name ;


package applicationContextAware;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Util implements ApplicationContextAware {
private static ApplicationContext context;

public void setApplicationContext(ApplicationContext context)
throws BeansException {
this.context = context;
public static ApplicationContext getContext() {
return context;


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<bean id="emp" class="applicationContextAware.Emp">
<property name="id" value="1" />
<property name="name" value value="ABC" />
<bean class="applicationContextAware.Util"/>

package applicationContextAware;

import org.springframework.context.ApplicationContext;

public class Main {

public static void main(String[] args) {

new ClassPathXmlApplicationContext(
ApplicationContext context = Util.getContext();
Emp emp = (Emp)context.getBean("emp");

In the above example you can see that we have create context object using Util class. First I have loaded Spring-Emp.xml file, and instantiate all beans. In Util Bean i have implements ApplicationContextAware for accessing ApplicationContext object using getContext() method. After that I have created one Emp object using context and print Emp class value.

     << Previous
Next >>