教程展示了如何在Spring应用程序中使用GenericApplicationContext 。在该示例中,我们创建了一个Spring Boot控制台应用程序。
Spring是一个流行的Java应用程序框架,Spring Boot 是Spring的演变,可以帮助您轻松创建独立的,基于生产级别的Spring应用程序。
GenericApplicationContext是一个实现ApplicationContext,它不预设指定任何bean定义格式; 例如XML或注释。
在下面的应用程序中,我们GenericApplicationContext 使用上下文的registerBean()方法创建并注册一个新bean 。稍后我们从应用程序上下文中检索bean getBean()。
以下是一个标准Spring Boot的POM.xml:
<?xml version=<font>"1.0"</font><font> encoding=</font><font>"UTF-8"</font><font>?>
<project xmlns=</font><font>"http://maven.apache.org/POM/4.0.0"</font><font>
xmlns:xsi=</font><font>"http://www.w3.org/2001/XMLSchema-instance"</font><font>
xsi:schemaLocation=</font><font>"http:</font><font><i>//maven.apache.org/POM/4.0.0 </i></font><font>
http:</font><font><i>//maven.apache.org/xsd/maven-4.0.0.xsd"></i></font><font>
<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>
</font>
这是Maven pom.xml文件。这spring-boot-starter-parent是一个父POM,为使用Maven构建的应用程序提供依赖性和插件管理。它spring-boot-starter是核心启动器,包括自动配置支持,日志记录和YAML。在spring-boot-starter-test春季增加了测试支持。将spring-boot-maven-pluginSpring应用程序包转换为可执行的JAR或WAR归档文件。
application.properties:
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:
<b>public</b> <b>class</b> TimeService {
<b>public</b> Instant getNow() {
<b>return</b> Instant.now();
}
}
TimeService包含一个返回当前日期和时间的简单方法。此服务类将在我们的通用应用程序上下文中注册。
@SpringBootApplication
<b>public</b> <b>class</b> MyApplication implements CommandLineRunner {
@Autowired
<b>private</b> GenericApplicationContext context;
<b>public</b> <b>static</b> <b>void</b> main(String[] args) {
SpringApplication.run(MyApplication.<b>class</b>, args);
}
@Override
<b>public</b> <b>void</b> run(String... args) throws Exception {
context.registerBean(<font>"com.zetcode.Service.TimeService"</font><font>,
TimeService.<b>class</b>, () -> <b>new</b> TimeService());
<b>var</b> timeService = (TimeService) context.getBean(TimeService.<b>class</b>);
System.out.println(timeService.getNow());
context.registerShutdownHook();
}
}
</font>
MyApplication是设置Spring Boot应用程序的入口点。该@SpringBootApplication注释能够自动配置和组件扫描。这是一个方便的注释,等同于@Configuration,@EnableAutoConfiguration以及@ComponentScan注释。
这里我们注入了GenericApplicationContext。使用该registerBean()方法注册了 一个新的TimeService bean 。
下面是测试MyApplicationTests.java:
@RunWith(SpringRunner.<b>class</b>)
@SpringBootTest
<b>public</b> <b>class</b> MyApplicationTests {
@Autowired
<b>private</b> GenericApplicationContext context;
@Test
<b>public</b> <b>void</b> testNow() {
<b>var</b> timeService = (TimeService) context.getBean(<font>"com.zetcode.Service.TimeService"</font><font>);
<b>var</b> now = timeService.getNow();
assertThat(now.isBefore(Instant.now()));
}
}
</font>
运行:
mvn -q spring-boot:run