原文: http://zetcode.com/springboot/genericapplicationcontext/

Spring Boot GenericApplicationContext教程展示了如何在 Spring 应用中使用GenericApplicationContext。 在示例中,我们创建一个 Spring Boot 控制台应用。

Spring 是流行的 Java 应用框架,而 Spring Boot 是 Spring 的演进,可以帮助轻松地创建独立的,生产级的基于 Spring 的应用。

GenericApplicationContext

GenericApplicationContextApplicationContext的实现,它不采用特定的 bean 定义格式; 例如 XML 或注解。

Spring Boot GenericApplicationContext示例

在以下应用中,我们创建一个GenericApplicationContext,并使用上下文的registerBean()方法注册一个新 bean。 稍后,我们使用getBean()从应用上下文中检索 bean。

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.zetcode</groupId>
  8. <artifactId>genappctx</artifactId>
  9. <version>0.0.1-SNAPSHOT</version>
  10. <packaging>jar</packaging>
  11. <name>genappctx</name>
  12. <description>Using GenericApplicationContext</description>
  13. <parent>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-parent</artifactId>
  16. <version>2.1.0.RELEASE</version>
  17. <relativePath/> <!-- lookup parent from repository -->
  18. </parent>
  19. <properties>
  20. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  21. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  22. <java.version>11</java.version>
  23. </properties>
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-test</artifactId>
  32. <scope>test</scope>
  33. </dependency>
  34. </dependencies>
  35. <build>
  36. <plugins>
  37. <plugin>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-maven-plugin</artifactId>
  40. </plugin>
  41. </plugins>
  42. </build>
  43. </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

  1. spring.main.banner-mode=off
  2. logging.level.root=ERROR
  3. logging.pattern.console=%d{dd-MM-yyyy HH:mm:ss} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n

application.properties是 Spring Boot 中的主要配置文件。 我们关闭 Spring 横幅,减少仅记录错误的错误,并设置控制台记录模式。

TimeService.java

  1. package com.zetcode.service;
  2. import java.time.Instant;
  3. public class TimeService {
  4. public Instant getNow() {
  5. return Instant.now();
  6. }
  7. }

TimeService包含一个返回当前日期和时间的简单方法。 该服务类将在我们的通用应用上下文中注册。

MyApplication.java

  1. package com.zetcode;
  2. import com.zetcode.service.TimeService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.CommandLineRunner;
  5. import org.springframework.boot.SpringApplication;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7. import org.springframework.context.support.GenericApplicationContext;
  8. @SpringBootApplication
  9. public class MyApplication implements CommandLineRunner {
  10. @Autowired
  11. private GenericApplicationContext context;
  12. public static void main(String[] args) {
  13. SpringApplication.run(MyApplication.class, args);
  14. }
  15. @Override
  16. public void run(String... args) throws Exception {
  17. context.registerBean("com.zetcode.Service.TimeService",
  18. TimeService.class, () -> new TimeService());
  19. var timeService = (TimeService) context.getBean(TimeService.class);
  20. System.out.println(timeService.getNow());
  21. context.registerShutdownHook();
  22. }
  23. }

MyApplication是设置 Spring Boot 应用的入口。 @SpringBootApplication注解启用自动配置和组件扫描。 它是@Configuration@EnableAutoConfiguration@ComponentScan注解的便捷注解。

  1. @Autowired
  2. private GenericApplicationContext context;

我们注入GenericApplicationContext

  1. context.registerBean("com.zetcode.Service.TimeService",
  2. TimeService.class, () -> new TimeService());

使用registerBean()方法注册了一个新的TimeService bean。

  1. var timeService = (TimeService) context.getBean(TimeService.class);

我们使用getBean()检索 bean。

  1. System.out.println(timeService.getNow());

最后,我们调用 bean 的getNow()方法。

MyApplicationTests.java

  1. package com.zetcode;
  2. import com.zetcode.service.TimeService;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.context.support.GenericApplicationContext;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import java.time.Instant;
  10. import static org.assertj.core.api.Assertions.assertThat;
  11. @RunWith(SpringRunner.class)
  12. @SpringBootTest
  13. public class MyApplicationTests {
  14. @Autowired
  15. private GenericApplicationContext context;
  16. @Test
  17. public void testNow() {
  18. var timeService = (TimeService) context.getBean("com.zetcode.Service.TimeService");
  19. var now = timeService.getNow();
  20. assertThat(now.isBefore(Instant.now()));
  21. }
  22. }

我们有一个使用TimeServicegetNow()方法的简单测试。

  1. var timeService = (TimeService) context.getBean("com.zetcode.Service.TimeService");

这次,我们通过给定名称引用 Bean。

  1. $ mvn -q spring-boot:run
  2. 2018-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 教程