一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Boot 自定義starter的示例代碼

Spring Boot 自定義starter的示例代碼

2021-06-15 11:00一花一四季,一夢一世界 Java教程

這篇文章主要介紹了Spring Boot 自定義starter的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

springboot 個人感覺特點:

1)眾多庫的集合(各種starter),方便快速構建應用系統。

2)自動配置spring(通過autoconfiguration機制),簡化配置,也方便擴展新的starter。

3)內嵌web容器,無需war部署。

創建一個用maven構建的springboot項目

Spring Boot 自定義starter的示例代碼

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 Boot 自定義starter的示例代碼

在新建的spring.factories文件中配置自動啟動類為我們之前編寫的helloautoconfiguration 類

org.springframework.boot.autoconfigure.enableautoconfiguration=com.xjw.helloautoconfiguration

Spring Boot 自定義starter的示例代碼

然后就可以在其他的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/將會看到如下結果

Spring Boot 自定義starter的示例代碼

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/cz-xjw/p/6632402.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本人成年视频在线观看 | 成人观看免费大片在线观看 | 88av免费观看| 国内精品久久久久久中文字幕 | 男生同性啪视频在线观看 | 免费日本在线视频 | 欧美老肥妇bbb | 99热在这里只有精品 | 色偷偷亚洲综合网亚洲 | 黄色大片网| 天天做天天爱天天爽综合区 | np小说h| 国产在线观看网站 | 亚洲不卡视频 | 国产成人综合精品 | 四虎影院地址 | 男人吃奶动态图 | chinese老太granny chinese国产人妖hd | 爆操俄罗斯美女 | 国产在亚洲线视频观看 | 特黄特级高清免费视频毛片 | 国产精品免费_区二区三区观看 | 五月天色综合 | 国产精品久久免费观看 | eee在线播放成人免费 | 亚洲成在人线久久综合 | 国内精品麻豆 | 韩国三级理韩国三级理人伦 | kk4kk免费毛片 | 91精品国产高清久久久久 | 亚洲AV无码A片在线观看蜜桃 | 男人插曲女人身体 | 1986葫芦兄弟全集免费观看第十集 | 俄罗斯妈妈k8影院在线观看 | 欧美艳星kagneyiynn高清 | 国产成人小视频在线观看 | 九九热视频免费观看 | 国产性片在线观看 | 欧美一区二区三区不卡视频 | 免费一区二区 | 亚洲AV久久久噜噜噜久久 |