springboot 個人感覺特點:
1)眾多庫的集合(各種starter),方便快速構建應用系統。
2)自動配置spring(通過autoconfiguration機制),簡化配置,也方便擴展新的starter。
3)內嵌web容器,無需war部署。
創建一個用maven構建的springboot項目
pom文件配置如下:
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
51
52
53
54
55
|
<?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.xjw.springboot</groupid> <artifactid>hellostarter</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>hello-spring-boot-starter</name> <description>測試自定義starter</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 2 .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> 1.8 </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> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-configuration-processor</artifactid> <optional> true </optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
定義一個pojo用來接收properties中配置的信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.xjw; import org.springframework.boot.context.properties.configurationproperties; @configurationproperties (prefix = "hello" ) public class helloserviceproperteis { private string msg; public string getmsg() { return msg; } public void setmsg(string msg) { this .msg = msg; } } |
@configurationproperties:用來標識這個pojo是一個用來接收指定前綴的資源配置值
prefix:表示在配置文件中配置項前綴[/code]
編寫一個service用來對外提供服務
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.xjw; public class helloservice { private string msg; public string sayhello() { return "hello " + msg; } public string getmsg() { return msg; } public void setmsg(string msg) { this .msg = msg; } } |
配置一個pojo用來讀取上面配置的helloserviceproperteis
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
|
package com.xjw; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.autoconfigure.condition.conditionalonclass; import org.springframework.boot.autoconfigure.condition.conditionalonmissingbean; import org.springframework.boot.autoconfigure.condition.conditionalonproperty; import org.springframework.boot.context.properties.enableconfigurationproperties; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; @configuration @enableconfigurationproperties (value = helloserviceproperteis. class ) @conditionalonclass (helloservice. class ) @conditionalonproperty (prefix = "hello" , value = "enable" , matchifmissing = true ) public class helloautoconfiguration { @autowired private helloserviceproperteis helloserviceproperteis; @bean @conditionalonmissingbean (helloservice. class ) public helloservice helloservice() { helloservice helloservice = new helloservice(); helloservice.setmsg(helloserviceproperteis.getmsg()); return helloservice; } } |
@configuration:標識此類為一個spring配置類
@enableconfigurationproperties(value = helloserviceproperteis.class):啟動配置文件,value用來指定我們要啟用的配置類,可以有多個,多個時我們可以這么寫value={xxproperties1.class,xxproperteis2.class....}
@conditionalonclass(helloservice.class):表示當classpath下存在helloservice.class文件時改配置文件類才有效
@conditionalonproperty(prefix = "hello", value = "enable", matchifmissing = true):表示只有我們的配置文件是否配置了以hello為前綴的資源項值,并且在該資源項值為enable,如果沒有配置我們默認設置為enable[/code]
最后在src/main/resources 文件夾下新建文件夾 meta-inf,在新建的meta-inf文件夾下新建spring.factories
在新建的spring.factories文件中配置自動啟動類為我們之前編寫的helloautoconfiguration 類
org.springframework.boot.autoconfigure.enableautoconfiguration=com.xjw.helloautoconfiguration
然后就可以在其他的spring-boot項目中使用我們剛剛新建的starter了,我們來測試一下
在新建一個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
51
52
53
54
55
56
57
58
59
60
|
<?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.xjw.springboot</groupid> <artifactid>hellostarter.test</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>hello-spring-boot-starter-test</name> <description>測試自定義starter</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 2 .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> 1.8 </java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>com.xjw.springboot</groupid> <artifactid>hellostarter</artifactid> <version> 0.0 . 1 -snapshot</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-devtools</artifactid> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
然后我們直接在咋們的啟動類中中嘗試使用以下我們上面定義的starter提供的helloservice:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.xjw; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller @springbootapplication public class hellospringbootstartertestapplication { @autowired private helloservice helloservice; @requestmapping ( "/" ) public string index() { return helloservice.sayhello(); } public static void main(string[] args) { springapplication.run(hellospringbootstartertestapplication. class , args); } } |
接著我們修改測試項目中的application.properteis,加入如下配置:
1
2
3
4
5
|
debug= true server.port= 8888 #hello=enable hello.msg=測試starter |
最后啟動項目,觀察控制臺輸出的內容中依賴的starter,從positive matches下我們可以看到有這么一句:
helloautoconfiguration matched:
- @conditionalonclass found required class 'com.xjw.helloservice'; @conditionalonmissingclass did not find unwanted class (onclasscondition)
- @conditionalonproperty (hello.enable) matched (onpropertycondition)
或者我們打開項目依賴樹也能找到我們的starter ,這說明spring已經自動的啟動了我們的starter了,打開瀏覽器輸入地址:http://localhost:8888/將會看到如下結果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/cz-xjw/p/6632402.html