Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring - ApplicationContext

In Spring, the ApplicationContext is a central interface for providing configuration information to the application. It provides an advanced form of the BeanFactory, which is another core interface in Spring for the same purpose. The ApplicationContext offers additional features compared to BeanFactory, such as integration with Spring's AOP features, message resource handling, event propagation, and more.

Here are some of the key aspects and features of ApplicationContext:

1. Configuration Sources:

The ApplicationContext can be bootstrapped from various sources, such as XML configurations, annotation-based configurations, or Java-based configurations.

2. Bean Creation:

It controls the creation of application objects and wiring them together. Beans defined in the application context can be retrieved by their name or type.

3. Event Propagation:

The ApplicationContext provides a way to publish and listen to application events. Beans can publish events to the application context, and other beans can listen and react to those events.

4. Resource Access:

It provides a unified way to load resources, such as images, property files, etc., from both within an application and external sources.

5. Internationalization (i18n):

The application context provides support for text messages in multiple languages through MessageSource, which offers parameterized and localized message resolution.

6. Inheritance:

There can be multiple ApplicationContext instances in an application, and they can be set up hierarchically. A child context can access beans defined in the parent context, but not vice versa.

Common Implementations of ApplicationContext:

  • ClassPathXmlApplicationContext: Loads context definition from an XML file located in the classpath.

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

    ApplicationContext context = new FileSystemXmlApplicationContext("C:/myApp/beans.xml");
    
  • AnnotationConfigApplicationContext: For programmatic, Java-based container configuration using annotations.

    ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
    
  • WebApplicationContext: Specialized version of ApplicationContext for web applications.

Usage:

Once you have an ApplicationContext, you can retrieve beans from it:

MyBean bean = context.getBean("myBean", MyBean.class);

The ApplicationContext is usually instantiated once during application initialization and remains for the lifespan of the application.

In Spring Boot:

Spring Boot simplifies the usage of ApplicationContext. When you bootstrap a Spring Boot application using SpringApplication.run(), it returns an instance of ApplicationContext:

public static void main(String[] args) {
    ApplicationContext context = SpringApplication.run(MySpringBootApplication.class, args);
    MyBean bean = context.getBean(MyBean.class);
}

In summary, the ApplicationContext is a central part of the Spring Framework, encapsulating configuration, bean lifecycle management, and various other cross-cutting concerns. It acts as a container that instantiates, configures, and manages a set of beans defined for the application.

  1. Configuring ApplicationContext in Spring:

    • Configure ApplicationContext using Java configuration or XML configuration.
    @Configuration
    @ComponentScan("com.example")
    public class AppConfig {
    
        @Bean
        public MyBean myBean() {
            return new MyBean();
        }
    }
    
  2. How to access ApplicationContext in Spring:

    • Access ApplicationContext using ApplicationContextAware interface or @Autowired annotation.
    @Service
    public class MyService implements ApplicationContextAware {
    
        private static ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext context) throws BeansException {
            applicationContext = context;
        }
    
        public static MyBean getBean() {
            return applicationContext.getBean(MyBean.class);
        }
    }
    
  3. ApplicationContext initialization in Spring:

    • ApplicationContext can be initialized using Java configuration or XML configuration.
    public class MainApp {
    
        public static void main(String[] args) {
            AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AppConfig.class);
    
            MyBean myBean = context.getBean(MyBean.class);
            myBean.doSomething();
    
            context.close();
        }
    }
    
  4. ApplicationContext hierarchy in Spring:

    • ApplicationContexts can be organized into a hierarchy, where a child context inherits beans from its parent. This allows for modular and organized configuration.
    public class ParentConfig {
    
        @Bean
        public MyBean parentBean() {
            return new MyBean();
        }
    }
    
    @Configuration
    @Import(ParentConfig.class)
    public class ChildConfig {
    
        @Bean
        public AnotherBean anotherBean() {
            return new AnotherBean();
        }
    }
    
  5. Working with ApplicationContext in Spring Boot:

    • In Spring Boot, ApplicationContext is automatically created and configured. You can access it without explicit configuration.
    @SpringBootApplication
    public class MyApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    
        @Autowired
        private MyBean myBean;
    
        @PostConstruct
        public void init() {
            myBean.doSomething();
        }
    }
    
  6. ApplicationContextAware interface in Spring:

    • The ApplicationContextAware interface allows a bean to be aware of the ApplicationContext and perform actions during initialization.
    @Service
    public class MyAwareService implements ApplicationContextAware {
    
        private ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext context) throws BeansException {
            this.applicationContext = context;
        }
    
        public void doSomethingWithApplicationContext() {
            // Access and use the ApplicationContext
        }
    }