Application Context

Application context is an advanced container that provides several enterprise-level functionalities, some of which are:

  • Providing a means for resolving text messages, including support for internationalization of those messages.
  • Providing a generic way to load file resources, such as images.
  • Publishing events to beans, which are registered as listeners
  • Has been built upon the concept of the bean factory container.
  • Provides other functionalities, such as easy integration with Spring AOP features and support for internationalization.
  • Is defined by the org.springframework.context. ApplicationContext interface.

In addition, it inherits all the capabilities of the BeanFactory interface, such as:

  • Creating the beans.
  • Associating them with each other.
  • Dispersing them to the users as per requests.

The Spring framework provides the following commonly used implementations of the application context container:

  • ClassPathXmlApplicationContext
  • FileSystemXmlApplicationContext
  • XmlWebApplicationContext

A major difference between the application context container and the bean factory container:

  • The bean factory container loads all the beans lazily, which means that the beans are created only when you call the getBean() method.
  • The application context container does not wait for the getBean() method to be called; it preloads all the beans as soon as the application context starts up.
Spring Application Context

Files Required For Spring Application Are

  • Spring Jar Files
  • MyApp Class
  • Triangle Class
  • Spring.xml

First Create New Java Project SpringDemo1 in Eclipse

Spring Application Context

Copy jar files to project and add jar files to build path

Spring Application Context

Create MyApp class in org.manish.javasafari package

Spring Application Context

Create Triangle class in org.manish.javasafari package

Spring Application Context

Add code to Tringle Class

Spring Application Context

Create spring.xml file in src folder and add code to it

Spring Application Context

Add code to MyApp class

In MyApp class we are not hard coding triangle object instead using bean factory for creating object from configuration file i.e. spring.xml.This is how we are decoupling dependency.

Spring Application Context

Run the application and see the output

Spring Application Context