Spring Boot GenericApplicationContext
last modified July 6, 2020
Spring Boot GenericApplicationContext tutorial shows how to use the
GenericApplicationContext
in a Spring application. In the
example, we create a Spring Boot console application.
Spring is a popular Java application framework and Spring Boot is an evolution of Spring that helps create stand-alone, production-grade Spring based applications easily.
GenericApplicationContext
GenericApplicationContext
is an implementation of the ApplicationContext
,
which does not assume a specific bean definition format; e.g. XML or annotations.
Spring Boot GenericApplicationContext example
In the following application we create a GenericApplicationContext
and register a new bean with the context's registerBean()
method.
Later we retrieve the bean from the application context with getBean()
.
<?xml version="1.0" encoding="UTF-8"?> <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>com.zetcode</groupId> <artifactId>genappctx</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>genappctx</name> <description>Using GenericApplicationContext</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
This is the Maven pom.xml
file. The spring-boot-starter-parent
is a parent
POM providing dependency and plugin management for applications built with Maven.
The spring-boot-starter
is a core starter, including auto-configuration support, logging, and YAML.
The spring-boot-starter-test
adds testing support in Spring.
The spring-boot-maven-plugin
packages Spring applications into executable JAR or WAR archives.
spring.main.banner-mode=off logging.level.root=ERROR logging.pattern.console=%d{dd-MM-yyyy HH:mm:ss} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n
The application.properties
is the main configuration file in Spring Boot.
We turn off the Spring banner, reduce the amount of logging to errors only, and set the
console logging pattern.
package com.zetcode.service; import java.time.Instant; public class TimeService { public Instant getNow() { return Instant.now(); } }
TimeService
contains a simple method which returns current date and time.
This service class is going to be registered in our generic application context.
package com.zetcode; import com.zetcode.service.TimeService; 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.support.GenericApplicationContext; @SpringBootApplication public class MyApplication implements CommandLineRunner { @Autowired private GenericApplicationContext context; public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { context.registerBean("com.zetcode.Service.TimeService", TimeService.class, () -> new TimeService()); var timeService = (TimeService) context.getBean(TimeService.class); System.out.println(timeService.getNow()); context.registerShutdownHook(); } }
MyApplication
is the entry point which sets up Spring Boot
application. The @SpringBootApplication
annotation enables
auto-configuration and component scanning. It is a convenience annotation
for @Configuration
, @EnableAutoConfiguration
,
and @ComponentScan
annotations.
@Autowired private GenericApplicationContext context;
We inject the GenericApplicationContext
.
context.registerBean("com.zetcode.Service.TimeService", TimeService.class, () -> new TimeService());
A new TimeService
bean is registered with the registerBean()
method.
var timeService = (TimeService) context.getBean(TimeService.class);
We retrieve the bean with getBean()
.
System.out.println(timeService.getNow());
Finally, we call the bean's getNow()
method.
package com.zetcode; import com.zetcode.service.TimeService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.support.GenericApplicationContext; import org.springframework.test.context.junit4.SpringRunner; import java.time.Instant; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest public class MyApplicationTests { @Autowired private GenericApplicationContext context; @Test public void testNow() { var timeService = (TimeService) context.getBean("com.zetcode.Service.TimeService"); var now = timeService.getNow(); assertThat(now.isBefore(Instant.now())); } }
We have a simple test that uses the TimeService's
getNow()
method.
var timeService = (TimeService) context.getBean("com.zetcode.Service.TimeService");
This time we refer to the bean by its given name.
$ mvn -q spring-boot:run 2018-11-24T16:31:32.146393700Z
We run the application.
In this tutorial, we have shown how to use GenericApplicationContext
in a Spring application.