The ApplicationContext is the main interface that provides configuration for an application. We can get ApplicationContext in Spring Boot.
The maven dependency and application.properties file.
maven dependency.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>modifying</groupId> <artifactId>modifying</artifactId> <version>0.0.1-SNAPSHOT</version> <name>modifying</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
application.properties
server.port = 9091
spring.application.name = applicationcontextexample
Get ApplicationContext using @Autowire annotation.
We can get the ApplicationContext, injecting it using @Autowired.
@Autowired
private ApplicationContext applicationContext;
Let’s see an example.
package com.javatute.main; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = "com.javatute.*") public class SpringMain implements CommandLineRunner { @Autowired private ApplicationContext applicationContext; public static void main(String[] args) { SpringApplication.run(SpringMain.class, args); } @Override public void run(String... args) throws Exception { System.out.println("applicationContext " + applicationContext); System.out.println("Application strat up date is: " + applicationContext.getStartupDate()); System.out.println("Total bean definition count: " + applicationContext.getBeanDefinitionCount()); System.out.println("ApplicationContext Id is: " + applicationContext.getId()); } }
Output on console is –
applicationContext: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6328d34a: startup date [Sun Jun 28 13:04:03 IST 2020]; root of context hierarchy
Application strat up date is: 1593329643442
Total bean definition count: 120
ApplicationContext Id is: applicationcontextexample
Get ApplicationContext using SpringApplication.run() method.
ApplicationContext applicationContext = SpringApplication.run(SpringMain.class, args);
package com.javatute.main; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = "com.javatute.*") public class SpringMain { public static void main(String[] args) { ApplicationContext applicationContext = SpringApplication.run(SpringMain.class, args); System.out.println("applicationContext " + applicationContext); System.out.println("Application Name is: " + applicationContext.getApplicationName()); System.out.println("Application strat up date is: " + applicationContext.getStartupDate()); System.out.println("Total bean definition count: " + applicationContext.getBeanDefinitionCount()); System.out.println("Application Display Name is: " + applicationContext.getDisplayName()); System.out.println("ApplicationContext Id is: " + applicationContext.getId()); } }
Output is –
applicationContext: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6328d34a: startup date [Sun Jun 28 13:04:03 IST 2020]; root of context hierarchy
Application strat up date is: 1593329643442
Total bean definition count: 120
ApplicationContext Id is: applicationcontextexample
Get ApplicationContext Using the ApplicationContextAware interface.
Directory Structure.
ApplicationContextInitializer.java
package com.javatute.main; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class ApplicationContextInitializer implements ApplicationContextAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } public ApplicationContext getApplicationContext(){ return applicationContext; } }
package com.javatute.main; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = "com.javatute.*") public class SpringMain implements CommandLineRunner{ @Autowired ApplicationContextInitializer applicationContextInitializer; public static void main(String[] args) { SpringApplication.run(SpringMain.class, args); } @Override public void run(String... args) throws Exception { ApplicationContext applicationContext = applicationContextInitializer.getApplicationContext(); System.out.println("applicationContext " + applicationContext); System.out.println("Application strat up date is: " + applicationContext.getStartupDate()); System.out.println("Total bean definition count: " + applicationContext.getBeanDefinitionCount()); System.out.println("ApplicationContext Id is: " + applicationContext.getId()); } }
The output would be the same.
Some more examples of ApplicationContext using Spring Boot.
ApplicationContext interface is available in org.springframework.context package.
Using ApplicationContext we can get Bean factory methods to access application components. Since ApplicatioContext extends the ListableBeanFactory interface and further ListableBeanFactory interface extends the BeanFactory interface.
Example to get the bean from ApplicationContext using getBean() method.
AppConfig.java
package com.javatute.main; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public Student student(){ return new Student("Chacha Chaudhry"); } }
Student.java
package com.javatute.main; public class Student { private String name; Student(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
SpringMain.java
package com.javatute.main; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = "com.javatute.*") public class SpringMain{ public static void main(String[] args) { ApplicationContext applicationContext = SpringApplication.run(SpringMain.class, args); Student student = (Student)applicationContext.getBean("student"); System.out.println("Student name is: "+student.getName()); } }
Output is –
Student name is: Chacha Chaudhry
Note – In the above example, we can pass Student.class instead of the “student” as a String.
Student student = (Student)applicationContext.getBean(Student.class);
We can also get a BeanFactory reference using ApplicationContext.
@SpringBootApplication @ComponentScan(basePackages = "com.javatute.*") public class SpringMain{ public static void main(String[] args) { ApplicationContext applicationContext = SpringApplication.run(SpringMain.class, args); BeanFactory beanFactory = applicationContext.getParentBeanFactory(); // To-DO } }
Use of ApplicationContext.
- It provides Bean factory methods for accessing application components.
- It allows loading file resources in a generic way and publishes events to registered listeners
- The ability to resolve messages, supporting internationalization.
That’s all about How to get ApplicationContext in Spring Boot.
Download source code from github.
You may like.
- How to get JPA EntityManager in Spring Boot.
- Spring Boot interceptor example.
- Filter example in Spring Boot.
Spring Data JPA examples.
- Spring Data JPA CrudRepository findById()
- Spring Data JPA JpaRepository getOne()
- Spring Data CrudRepository saveAll() and findAll().
- Spring Data CrudRepository existsById()
- Spring Data JPA delete() vs deleteInBatch()
- Spring Data JPA deleteAll() Vs deleteAllInBatch()
- Spring Data JPA JpaRepository deleteAllInBatch()
- Spring Data JPA deleteInBatch() Example
- Spring Data JPA JpaRepository saveAndFlush() Example
- Spring Data JPA CrudRepository count() Example.
Hibernate association mapping using Spring Boot.
- One To One Mapping Annotation Example in Hibernate/JPA using Spring Boot and Oracle.
- One To One Bidirectional Mapping Example In Hibernate/JPA Using Spring Boot and Oracle.
- One To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And Oracle.
- Many To One Unidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.
- One To Many Bidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.
- Many To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And Oracle.
- Hibernate/JPA Single Table Inheritance Mapping Example Using Spring Boot and Oracle.
- Hibernate/JPA Table Per Subclass Mapping Example Using Spring Boot And Oracle.
- Hibernate/JPA Table Per Concrete Class Mapping Example Using Spring Boot And Oracle.
ApplicationContext docs.