Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Spring Application Context

The Spring Application Context is a central interface to the Spring IoC container. It provides configuration information and manages the lifecycle of beans in a Spring application. This guide covers the key concepts, types, and features of the Spring Application Context.

What is Application Context?

The Application Context is a sophisticated container that provides all the functionality of BeanFactory, plus additional features such as event propagation, declarative mechanisms to create a bean, and various means to look up. It is the central interface to the Spring IoC container and is used to manage beans in a Spring application.

Types of Application Context

Spring provides several implementations of the Application Context interface to suit different needs:

  • ClassPathXmlApplicationContext: Loads context definition from an XML file located in the classpath.
  • FileSystemXmlApplicationContext: Loads context definition from an XML file in the file system.
  • AnnotationConfigApplicationContext: Loads context definition from Java-based configuration classes annotated with @Configuration.
  • WebApplicationContext: A specialized version of the Application Context for web applications.

Loading Application Context

Depending on the type of application context you use, you load it in different ways:

ClassPathXmlApplicationContext

// Loading context from classpath XML
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyBean myBean = context.getBean(MyBean.class);
myBean.doSomething();

FileSystemXmlApplicationContext

// Loading context from file system XML
ApplicationContext context = new FileSystemXmlApplicationContext("C:/path/to/applicationContext.xml");
MyBean myBean = context.getBean(MyBean.class);
myBean.doSomething();

AnnotationConfigApplicationContext

// Loading context from Java-based configuration
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyBean myBean = context.getBean(MyBean.class);
myBean.doSomething();

Bean Definition and Retrieval

Beans are defined in the configuration file (XML or Java-based) and retrieved from the application context:

// applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="myBean" class="com.example.MyBean"/>
</beans>

// AppConfig.java
@Configuration
public class AppConfig {
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

// Retrieving beans
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyBean myBean = context.getBean(MyBean.class);
myBean.doSomething();

ApplicationContext javaContext = new AnnotationConfigApplicationContext(AppConfig.class);
MyBean javaBean = javaContext.getBean(MyBean.class);
javaBean.doSomething();

Event Handling

The Application Context supports event handling, allowing beans to publish and listen to events:

// Custom event
public class MyEvent extends ApplicationEvent {
    public MyEvent(Object source) {
        super(source);
    }
}

// Event publisher
@Component
public class MyEventPublisher {
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public void publishEvent() {
        MyEvent myEvent = new MyEvent(this);
        applicationEventPublisher.publishEvent(myEvent);
    }
}

// Event listener
@Component
public class MyEventListener implements ApplicationListener {
    @Override
    public void onApplicationEvent(MyEvent event) {
        System.out.println("Received event: " + event);
    }
}

// Publishing and listening to events
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyEventPublisher publisher = context.getBean(MyEventPublisher.class);
publisher.publishEvent();

Internationalization (i18n)

The Application Context supports internationalization by providing message sources for resolving messages:

// messages.properties
greeting=Hello, World!

// AppConfig.java
@Configuration
public class AppConfig {
    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }
}

// Retrieving messages
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
String greeting = context.getMessage("greeting", null, Locale.ENGLISH);
System.out.println(greeting); // Output: Hello, World!

Key Points

  • The Application Context is the central interface to the Spring IoC container, managing the lifecycle of beans.
  • Spring provides various implementations of the Application Context, such as ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, and AnnotationConfigApplicationContext.
  • Beans are defined in configuration files and retrieved from the Application Context.
  • The Application Context supports event handling, allowing beans to publish and listen to events.
  • The Application Context supports internationalization by providing message sources for resolving messages.

Conclusion

The Spring Application Context is a powerful and flexible mechanism for managing beans and their lifecycle in a Spring application. By leveraging its features, such as bean definition and retrieval, event handling, and internationalization, developers can create robust and maintainable applications. Happy coding!