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:
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,
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
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.