原文: http://zetcode.com/springboot/genericapplicationcontext/
Spring Boot GenericApplicationContext教程展示了如何在 Spring 应用中使用GenericApplicationContext。 在示例中,我们创建一个 Spring Boot 控制台应用。
Spring 是流行的 Java 应用框架,而 Spring Boot 是 Spring 的演进,可以帮助轻松地创建独立的,生产级的基于 Spring 的应用。
GenericApplicationContext
GenericApplicationContext是ApplicationContext的实现,它不采用特定的 bean 定义格式; 例如 XML 或注解。
Spring Boot GenericApplicationContext示例
在以下应用中,我们创建一个GenericApplicationContext,并使用上下文的registerBean()方法注册一个新 bean。 稍后,我们使用getBean()从应用上下文中检索 bean。
pom.xml
<?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.0http://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>
这是 Maven pom.xml文件。 spring-boot-starter-parent是父 POM,它为使用 Maven 构建的应用提供依赖关系和插件管理。 spring-boot-starter是一个核心启动器,包括自动配置支持,日志记录和 YAML。 spring-boot-starter-test在 Spring 中增加了测试支持。 spring-boot-maven-plugin将 Spring 应用打包到可执行的 JAR 或 WAR 归档文件中。
application.properties
spring.main.banner-mode=offlogging.level.root=ERRORlogging.pattern.console=%d{dd-MM-yyyy HH:mm:ss} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n
application.properties是 Spring Boot 中的主要配置文件。 我们关闭 Spring 横幅,减少仅记录错误的错误,并设置控制台记录模式。
TimeService.java
package com.zetcode.service;import java.time.Instant;public class TimeService {public Instant getNow() {return Instant.now();}}
TimeService包含一个返回当前日期和时间的简单方法。 该服务类将在我们的通用应用上下文中注册。
MyApplication.java
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;@SpringBootApplicationpublic class MyApplication implements CommandLineRunner {@Autowiredprivate GenericApplicationContext context;public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}@Overridepublic 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是设置 Spring Boot 应用的入口。 @SpringBootApplication注解启用自动配置和组件扫描。 它是@Configuration,@EnableAutoConfiguration和@ComponentScan注解的便捷注解。
@Autowiredprivate GenericApplicationContext context;
我们注入GenericApplicationContext。
context.registerBean("com.zetcode.Service.TimeService",TimeService.class, () -> new TimeService());
使用registerBean()方法注册了一个新的TimeService bean。
var timeService = (TimeService) context.getBean(TimeService.class);
我们使用getBean()检索 bean。
System.out.println(timeService.getNow());
最后,我们调用 bean 的getNow()方法。
MyApplicationTests.java
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)@SpringBootTestpublic class MyApplicationTests {@Autowiredprivate GenericApplicationContext context;@Testpublic 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。
$ mvn -q spring-boot:run2018-11-24T16:31:32.146393700Z
我们运行该应用。
在本教程中,我们展示了如何在 Spring 应用中使用GenericApplicationContext。 您可能也对相关教程感兴趣: Spring Boot @PostConstruct教程, Spring Boot @Controller教程, Spring Boot @ExceptionHandler教程, Spring Boot 上传文件, Spring Boot @PathVariable教程, Spring Boot @RequestParam教程, Spring Boot @ResponseBody教程, Java 教程。
