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
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.
Bean Instantiation and Configuration: ApplicationContext
allows you to define and manage beans, their dependencies, and their lifecycle.
Event Propagation: It supports a generic event mechanism, which means beans can listen for events or publish events to other beans.
Internationalization (i18n): Provides a way to resolve messages, supporting internationalization.
Resource Access: Offers a generic way to load resources, such as images, properties files, etc.
Load multiple configurations: You can load configurations from multiple sources using ApplicationContext
.
Web Applications: Some specialized implementations like WebApplicationContext
integrate with the web applications, allowing access to web-specific features.
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");
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");
AnnotationConfigApplicationContext: Accepts annotated classes as input, typically using @Configuration
classes.
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
WebApplicationContext: Designed for web applications and adds some web-specific context features, like the ability to resolve themes.
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.
Using ClassPathXmlApplicationContext for XML-based configuration in Spring:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AnnotationConfigApplicationContext for Java-based configuration in Spring:
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Loading multiple configuration files in ApplicationContext in Spring:
ApplicationContext context = new ClassPathXmlApplicationContext("file1.xml", "file2.xml");
Accessing beans from ApplicationContext in a Spring application:
MyBean myBean = context.getBean("myBean", MyBean.class);
Using ApplicationContextAware interface for bean awareness in Spring:
ApplicationContextAware
for access to ApplicationContext:public class MyBean implements ApplicationContextAware { private ApplicationContext context; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }
Handling profiles and environments in ApplicationContext:
@Profile
annotation or set profiles in XML:@Configuration @Profile("dev") public class DevConfig { // Dev-specific configuration }