๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

ApplicationContext - Spring Container


Introduction

  • ApplicationContext is a more "Advanced IoC (Inversion of Control) container" in the Spring framework (as compared to BeanFactory).
  • It eagerly instantiates singleton beans during container startup, making it suitable for larger and more complex applications.
  • It is built on the basic functionality provided by BeanFactory and adds features such as support for AOP (Aspect-Oriented Programming), internationalization, event propagation, message sources, declarative transactions etc.
  • Note : ApplicationContext is an interface, and its concrete implementations (such as ClassPathXmlApplicationContext or AnnotationConfigApplicationContext) act as actual Spring containers, responsible for managing the lifecycle and dependencies of beans.

Interview Question : What is Difference between BeanFactory & ApplicationContext ?
Click Here for answer


When to use ApplicationContext ?

ApplicationContext is mostly used in web applications with Spring MVC.


Advantages of ApplicationContext

  1. Eager Bean Initialization : Singleton beans are created at the time of context initialization, ensuring that any configuration issues are caught early.
  2. Internationalization (i18n) Support : It provides support for message sources to handle localization (multi-language) requirements.
  3. Built-in Support for AOP (Aspect-Oriented Programming) : You can apply cross-cutting concerns like logging, security, or performance monitoring using AOP.
  4. Easy Integration with Web Applications : Special implementations of ApplicationContext such as WebApplicationContext provide tight integration with web frameworks like Spring MVC.
  5. Declarative Transaction Management : It enables the use of annotations like @Transactional for transaction management, ensuring easier handling of transactions in services.

Limitations of ApplicationContext

  1. Higher Memory Consumption : Because singleton beans are initialized eagerly, it consumes more memory at startup compared to BeanFactory.
  2. Longer Startup Time : Eager initialization of beans can increase startup time, especially in applications with many beans.
  3. Overhead for Simple Applications : For small or lightweight applications, the features provided by ApplicationContext may be unnecessary, and BeanFactory would be a simpler alternative.
  4. Complex Configuration : For beginners, the wide range of features and configuration options might seem overwhelming.

Note: ApplicationContext is "New Spring Container" as compared to BeanFactory, so we should always use ApplicationContext in real world projects.


Implemented Classes of ApplicationContext

  1. ClassPathXmlApplicationContext : Loads the Spring configuration from an XML file located in the classpath.
    Syntax of creating ClassPathXmlApplicationContext container is as below :
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  2. FileSystemXmlApplicationContext : Loads the configuration from an XML file in the file system (outside the classpath).
    Syntax of creating FileSystemXmlApplicationContext container is as below :
    ApplicationContext context = new FileSystemXmlApplicationContext("/path/to/beans.xml");
  3. AnnotationConfigApplicationContext : Used for Java-based configuration, where beans are defined using annotations instead of XML.
    Syntax of creating AnnotationConfigApplicationContext container is as below :
    ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
  4. WebApplicationContext : A specialized version of ApplicationContext designed for web applications. It integrates with ServletContext and is typically used in Spring MVC.
  5. GenericApplicationContext : A flexible container that supports both XML-based and Java-based configurations.