企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Spring Boot `GenericApplicationContext` > 原文: [http://zetcode.com/springboot/genericapplicationcontext/](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` ```java <?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> ``` 这是 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` ```java 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 横幅,减少仅记录错误的错误,并设置控制台记录模式。 `TimeService.java` ```java package com.zetcode.service; import java.time.Instant; public class TimeService { public Instant getNow() { return Instant.now(); } } ``` `TimeService`包含一个返回当前日期和时间的简单方法。 该服务类将在我们的通用应用上下文中注册。 `MyApplication.java` ```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; @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`是设置 Spring Boot 应用的入口。 `@SpringBootApplication`注解启用自动配置和组件扫描。 它是`@Configuration`,`@EnableAutoConfiguration`和`@ComponentScan`注解的便捷注解。 ```java @Autowired private GenericApplicationContext context; ``` 我们注入`GenericApplicationContext`。 ```java context.registerBean("com.zetcode.Service.TimeService", TimeService.class, () -> new TimeService()); ``` 使用`registerBean()`方法注册了一个新的`TimeService` bean。 ```java var timeService = (TimeService) context.getBean(TimeService.class); ``` 我们使用`getBean()`检索 bean。 ```java System.out.println(timeService.getNow()); ``` 最后,我们调用 bean 的`getNow()`方法。 `MyApplicationTests.java` ```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) @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())); } } ``` 我们有一个使用`TimeService`的`getNow()`方法的简单测试。 ```java var timeService = (TimeService) context.getBean("com.zetcode.Service.TimeService"); ``` 这次,我们通过给定名称引用 Bean。 ```java $ mvn -q spring-boot:run 2018-11-24T16:31:32.146393700Z ``` 我们运行该应用。 在本教程中,我们展示了如何在 Spring 应用中使用`GenericApplicationContext`。 您可能也对相关教程感兴趣: [Spring Boot `@PostConstruct`教程](/springboot/postconstruct/), [Spring Boot `@Controller`教程](/springboot/controller/), [Spring Boot `@ExceptionHandler`教程](/springboot/exceptionhandler/), [Spring Boot 上传文件](/springboot/uploadfile/), [Spring Boot `@PathVariable`教程](/springboot/pathvariable/), [Spring Boot `@RequestParam`教程](/springboot/requestparam/), [Spring Boot `@ResponseBody`教程](/springboot/responsebody/), [Java 教程](/lang/java/)。