Spring Boot GenericApplicationContext
最后修改于 2023 年 7 月 28 日
Spring Boot GenericApplicationContext 教程展示了如何在 Spring 应用中使用 GenericApplicationContext。在示例中,我们创建了一个 Spring Boot 控制台应用程序。
Spring 是一个流行的 Java 应用程序框架,而 Spring Boot 是 Spring 的一个演进,它有助于轻松创建独立的、生产级的基于 Spring 的应用程序。
GenericApplicationContext
GenericApplicationContext 是 ApplicationContext 的一个实现,它不假设特定的 bean 定义格式;例如 XML 或注解。
Spring Boot GenericApplicationContext 示例
在下面的应用程序中,我们创建了一个 GenericApplicationContext,并使用 context 的 registerBean 方法注册了一个新的 bean。稍后,我们使用 getBean 从应用程序上下文中检索该 bean。
plugins {
id 'org.springframework.boot' version '3.1.1'
id 'io.spring.dependency-management' version '1.1.0'
id 'java'
}
group = 'com.zetcode'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
这是 Gradle 构建文件。spring-boot-starter 是一个核心启动器,包括自动配置支持、日志记录和 YAML。spring-boot-starter-test 在 Spring 中添加了测试支持。
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
application.properties 是 Spring Boot 中的主要配置文件。我们关闭了 Spring 标语,将日志记录量减少到仅错误,并设置了控制台日志记录模式。
package com.zetcode.service;
import java.time.Instant;
public class TimeService {
public Instant getNow() {
return Instant.now();
}
}
TimeService 包含一个简单的方法,该方法返回当前日期和时间。这个服务类将在我们的通用应用程序上下文中注册。
package com.zetcode;
import com.zetcode.service.TimeService;
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 Application implements CommandLineRunner {
private final GenericApplicationContext context;
public Application(GenericApplicationContext context) {
this.context = context;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
context.registerBean("com.zetcode.Service.TimeService",
TimeService.class, TimeService::new);
var timeService = (TimeService) context.getBean(TimeService.class);
System.out.println(timeService.getNow());
context.registerShutdownHook();
}
}
Application 是设置 Spring Boot 应用程序的入口点。@SpringBootApplication 注解启用了自动配置和组件扫描。它是 @Configuration、@EnableAutoConfiguration 和 @ComponentScan 注解的便捷注解。
private final GenericApplicationContext context;
public Application(GenericApplicationContext context) {
this.context = context;
}
我们注入了 GenericApplicationContext。
context.registerBean("com.zetcode.Service.TimeService",
TimeService.class, TimeService::new);
一个新的 TimeService bean 使用 registerBean 方法注册。
var timeService = (TimeService) context.getBean(TimeService.class);
我们使用 getBean 检索 bean。
System.out.println(timeService.getNow());
最后,我们调用该 bean 的 getNow 方法。
package com.zetcode;
import com.zetcode.service.TimeService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.support.GenericApplicationContext;
import java.time.Instant;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
public class ApplicationTests {
@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()));
}
}
我们有一个简单的测试,使用了 TimeService 的 getNow 方法。
var timeService = (TimeService) context.getBean("com.zetcode.service.TimeService");
这次我们通过其给定名称引用 bean。
$ ./gradlew bootRun ... ... INFO Application MyApp
我们运行应用程序。
在本文中,我们展示了如何在 Spring 应用程序中使用 GenericApplicationContext。