Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
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
:
The ApplicationContext
can be bootstrapped from various sources, such as XML configurations, annotation-based configurations, or Java-based configurations.
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.
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.
It provides a unified way to load resources, such as images, property files, etc., from both within an application and external sources.
The application context provides support for text messages in multiple languages through MessageSource
, which offers parameterized and localized message resolution.
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.
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.
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.
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.
Configuring ApplicationContext in Spring:
@Configuration @ComponentScan("com.example") public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } }
How to access ApplicationContext in Spring:
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); } }
ApplicationContext initialization in Spring:
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(); } }
ApplicationContext hierarchy in Spring:
public class ParentConfig { @Bean public MyBean parentBean() { return new MyBean(); } } @Configuration @Import(ParentConfig.class) public class ChildConfig { @Bean public AnotherBean anotherBean() { return new AnotherBean(); } }
Working with ApplicationContext in Spring Boot:
@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(); } }
ApplicationContextAware interface in Spring:
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 } }