教程展示了如何在spring應用程序中使用genericapplicationcontext 。在該示例中,我們創建了一個spring boot控制臺應用程序。
spring是一個流行的java應用程序框架,spring boot 是spring的演變,可以幫助您輕松創建獨立的,基于生產級別的spring應用程序。
genericapplicationcontext是一個實現applicationcontext,它不預設指定任何bean定義格式; 例如xml或注釋。
在下面的應用程序中,我們genericapplicationcontext 使用上下文的registerbean()方法創建并注冊一個新bean 。稍后我們從應用程序上下文中檢索bean getbean()。
以下是一個標準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是一個父pom,為使用maven構建的應用程序提供依賴性和插件管理。它spring-boot-starter是核心啟動器,包括自動配置支持,日志記錄和yaml。在spring-boot-starter-test春季增加了測試支持。將spring-boot-maven-pluginspring應用程序包轉換為可執行的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 |
這個application.properties是spring boot中的主要配置文件。我們關閉spring標題,僅減少記錄到錯誤的數量,并設置控制臺日志記錄模式。
timeservice.java:
1
2
3
4
5
6
7
|
public class timeservice { public instant getnow() { return instant.now(); } } |
timeservice包含一個返回當前日期和時間的簡單方法。此服務類將在我們的通用應用程序上下文中注冊。
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是設置spring boot應用程序的入口點。該@springbootapplication注釋能夠自動配置和組件掃描。這是一個方便的注釋,等同于@configuration,@enableautoconfiguration以及@componentscan注釋。
這里我們注入了genericapplicationcontext。使用該registerbean()方法注冊了 一個新的timeservice bean 。
下面是測試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())); } } |
運行:
mvn -q spring-boot:run
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jdon.com/50838