教程展示了如何在spring應(yīng)用程序中使用genericapplicationcontext 。在該示例中,我們創(chuàng)建了一個(gè)spring boot控制臺(tái)應(yīng)用程序。
spring是一個(gè)流行的java應(yīng)用程序框架,spring boot 是spring的演變,可以幫助您輕松創(chuàng)建獨(dú)立的,基于生產(chǎn)級(jí)別的spring應(yīng)用程序。
genericapplicationcontext是一個(gè)實(shí)現(xiàn)applicationcontext,它不預(yù)設(shè)指定任何bean定義格式; 例如xml或注釋。
在下面的應(yīng)用程序中,我們genericapplicationcontext 使用上下文的registerbean()方法創(chuàng)建并注冊(cè)一個(gè)新bean 。稍后我們從應(yīng)用程序上下文中檢索bean getbean()。
以下是一個(gè)標(biāo)準(zhǔn)spring boot的pom.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<?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是一個(gè)父pom,為使用maven構(gòu)建的應(yīng)用程序提供依賴性和插件管理。它spring-boot-starter是核心啟動(dòng)器,包括自動(dòng)配置支持,日志記錄和yaml。在spring-boot-starter-test春季增加了測(cè)試支持。將spring-boot-maven-pluginspring應(yīng)用程序包轉(zhuǎn)換為可執(zhí)行的jar或war歸檔文件。
application.properties:
1
2
3
|
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 |
這個(gè)application.properties是spring boot中的主要配置文件。我們關(guān)閉spring標(biāo)題,僅減少記錄到錯(cuò)誤的數(shù)量,并設(shè)置控制臺(tái)日志記錄模式。
timeservice.java:
1
2
3
4
5
6
7
|
public class timeservice { public instant getnow() { return instant.now(); } } |
timeservice包含一個(gè)返回當(dāng)前日期和時(shí)間的簡(jiǎn)單方法。此服務(wù)類將在我們的通用應(yīng)用程序上下文中注冊(cè)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@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是設(shè)置spring boot應(yīng)用程序的入口點(diǎn)。該@springbootapplication注釋能夠自動(dòng)配置和組件掃描。這是一個(gè)方便的注釋,等同于@configuration,@enableautoconfiguration以及@componentscan注釋。
這里我們注入了genericapplicationcontext。使用該registerbean()方法注冊(cè)了 一個(gè)新的timeservice bean 。
下面是測(cè)試myapplicationtests.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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())); } } |
運(yùn)行:
mvn -q spring-boot:run
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.jdon.com/50838