Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring - ApplicationContext

ApplicationContext is a central interface in Spring's Inversion of Control (IoC) container. It provides a way to access application objects and adds enterprise-specific functionality on top of the core BeanFactory interface. In essence, the ApplicationContext includes all the capabilities of BeanFactory, but also provides more with respect to application-layer features.

Key Features:

  1. Bean Instantiation and Configuration: ApplicationContext allows you to define and manage beans, their dependencies, and their lifecycle.

  2. Event Propagation: It supports a generic event mechanism, which means beans can listen for events or publish events to other beans.

  3. Internationalization (i18n): Provides a way to resolve messages, supporting internationalization.

  4. Resource Access: Offers a generic way to load resources, such as images, properties files, etc.

  5. Load multiple configurations: You can load configurations from multiple sources using ApplicationContext.

  6. Web Applications: Some specialized implementations like WebApplicationContext integrate with the web applications, allowing access to web-specific features.

Popular Implementations:

  1. ClassPathXmlApplicationContext: Loads the context definition from an XML file located in the classpath, treating context definitions as classpath resources.

    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    
  2. FileSystemXmlApplicationContext: Loads the context definition from an XML file in the filesystem or from a URL.

    ApplicationContext context = new FileSystemXmlApplicationContext("file:/path/to/beans.xml");
    
  3. AnnotationConfigApplicationContext: Accepts annotated classes as input, typically using @Configuration classes.

    ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
    
  4. WebApplicationContext: Designed for web applications and adds some web-specific context features, like the ability to resolve themes.

ApplicationContext vs BeanFactory:

While BeanFactory and ApplicationContext both provide a way to configure and manage beans, ApplicationContext offers more advanced features:

  • Application Events: Allows beans to receive events.

  • Specialized beans creation: Such as MessageSource, making internationalization possible.

  • Built-in Enterprise Support: For example, JNDI access, EJB integration, etc.

  • Web Application Context: For web applications.

It's worth noting that in most typical Spring applications, developers use ApplicationContext more often than BeanFactory due to its comprehensive feature set and ease of use with annotations and web applications.

  1. Using ClassPathXmlApplicationContext for XML-based configuration in Spring:

    • Create a context from an XML file:
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      
  2. AnnotationConfigApplicationContext for Java-based configuration in Spring:

    • Create a context from Java configuration:
      ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
      
  3. Loading multiple configuration files in ApplicationContext in Spring:

    • Use multiple files separated by commas:
      ApplicationContext context = new ClassPathXmlApplicationContext("file1.xml", "file2.xml");
      
  4. Accessing beans from ApplicationContext in a Spring application:

    • Retrieve beans by name or type:
      MyBean myBean = context.getBean("myBean", MyBean.class);
      
  5. Using ApplicationContextAware interface for bean awareness in Spring:

    • Implement ApplicationContextAware for access to ApplicationContext:
      public class MyBean implements ApplicationContextAware {
          private ApplicationContext context;
      
          @Override
          public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
              this.context = applicationContext;
          }
      }
      
  6. Handling profiles and environments in ApplicationContext:

    • Use @Profile annotation or set profiles in XML:
      @Configuration
      @Profile("dev")
      public class DevConfig {
          // Dev-specific configuration
      }