最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot 自定義starter的示例代碼

 更新時間:2018年11月22日 13:57:58   作者:一花一四季,一夢一世界  
這篇文章主要介紹了Spring Boot 自定義starter的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

SpringBoot 個人感覺特點(diǎn):

1)眾多庫的集合(各種Starter),方便快速構(gòu)建應(yīng)用系統(tǒng)。

2)自動配置spring(通過AutoConfiguration機(jī)制),簡化配置,也方便擴(kuò)展新的Starter。

3)內(nèi)嵌web容器,無需WAR部署。

創(chuàng)建一個用maven構(gòu)建的springboot項(xiàng)目

pom文件配置如下:

<?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中配置的信息

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:用來標(biāo)識這個pojo是一個用來接收指定前綴的資源配置值

prefix:表示在配置文件中配置項(xiàng)前綴[/code]

編寫一個Service用來對外提供服務(wù)

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

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:標(biāo)識此類為一個spring配置類

@EnableConfigurationProperties(value = HelloServiceProperteis.class):啟動配置文件,value用來指定我們要啟用的配置類,可以有多個,多個時我們可以這么寫value={xxProperties1.class,xxProperteis2.class....}

@ConditionalOnClass(HelloService.class):表示當(dāng)classPath下存在HelloService.class文件時改配置文件類才有效

@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true):表示只有我們的配置文件是否配置了以hello為前綴的資源項(xiàng)值,并且在該資源項(xiàng)值為enable,如果沒有配置我們默認(rèn)設(shè)置為enable[/code]

最后在src/main/resources 文件夾下新建文件夾 META-INF,在新建的META-INF文件夾下新建spring.factories

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

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xjw.HelloAutoConfiguration

然后就可以在其他的spring-boot項(xiàng)目中使用我們剛剛新建的starter了,我們來測試一下

在新建一個spring-boot項(xiàng)目,pom.xml配置如下:

<?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:

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);
  }
}

接著我們修改測試項(xiàng)目中的application.properteis,加入如下配置:

debug=true
server.port=8888

#hello=enable
hello.msg=測試starter

最后啟動項(xiàng)目,觀察控制臺輸出的內(nèi)容中依賴的starter,從Positive matches下我們可以看到有這么一句:

HelloAutoConfiguration matched:
- @ConditionalOnClass found required class 'com.xjw.HelloService'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
- @ConditionalOnProperty (hello.enable) matched (OnPropertyCondition)

或者我們打開項(xiàng)目依賴樹也能找到我們的starter ,這說明spring已經(jīng)自動的啟動了我們的starter了,打開瀏覽器輸入地址:http://localhost:8888/將會看到如下結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring-data-jpa使用自定義repository來實(shí)現(xiàn)原生sql

    spring-data-jpa使用自定義repository來實(shí)現(xiàn)原生sql

    這篇文章主要介紹了在spring-data-jpa中使用自定義repository來實(shí)現(xiàn)原生sql,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題

    解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題

    這篇文章主要介紹了解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • java實(shí)現(xiàn)圖片反色處理示例

    java實(shí)現(xiàn)圖片反色處理示例

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖片反色處理示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • idea的easyCode的 MybatisPlus模板的配置詳解

    idea的easyCode的 MybatisPlus模板的配置詳解

    這篇文章主要介紹了idea的easyCode的 MybatisPlus模板的配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 通過volatile驗(yàn)證線程之間的可見性

    通過volatile驗(yàn)證線程之間的可見性

    這篇文章主要介紹了通過volatile驗(yàn)證線程之間的可見性,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • Spring成員對象注入的三種方式詳解

    Spring成員對象注入的三種方式詳解

    這篇文章主要為大家詳細(xì)介紹了Spring成員對象注入的三種方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • 關(guān)于Java Interface接口的簡單練習(xí)題

    關(guān)于Java Interface接口的簡單練習(xí)題

    這篇文章主要給大家分享的是關(guān)于Java Interface接口的簡單練習(xí)題,難度不算大,但是要有一個清晰的邏輯建立接口和鏈接Java類。下面來看看文章的詳細(xì)介紹吧,需要的朋友可以參考一下
    2021-11-11
  • 淺談Spring注入模型

    淺談Spring注入模型

    如果不深入到Spring的源碼,是很少有機(jī)會了解到Spring的注入模型(AutowireMode)。但是為了掃清我們學(xué)習(xí)Spring源碼的障礙,我們有必要了解下Spring的注入模型,感興趣的同學(xué)可以閱讀一下
    2023-04-04
  • 分析xxljob登入功能集成OIDC的統(tǒng)一認(rèn)證

    分析xxljob登入功能集成OIDC的統(tǒng)一認(rèn)證

    這篇文章主要為大家介紹分析xxljob登入功能集成OIDC的統(tǒng)一認(rèn)證的詳解說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-02-02
  • JMagick實(shí)現(xiàn)基本圖像處理的類實(shí)例

    JMagick實(shí)現(xiàn)基本圖像處理的類實(shí)例

    這篇文章主要介紹了JMagick實(shí)現(xiàn)基本圖像處理的類,實(shí)例分析了java圖像處理的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06

最新評論

柏乡县| 和田县| 曲靖市| 吴川市| 米脂县| 成安县| 泗水县| 葫芦岛市| 闽侯县| 内黄县| 夏河县| 台北县| 龙口市| 都江堰市| 金秀| 米泉市| 张家口市| 扎鲁特旗| 海丰县| 衡阳市| 铜陵市| 丽江市| 大关县| 庆安县| 五指山市| 巴林左旗| 舒城县| 肃北| 博客| 杭锦后旗| 清远市| 顺义区| 塘沽区| 合山市| 大理市| 辽阳市| 来宾市| 苍山县| 攀枝花市| 遂川县| 健康|