Spring Boot的GenericApplicationContext使用教程
教程展示瞭如何在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