成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

Springboot怎么創(chuàng)建自定義starter

小編給大家分享一下Spring boot怎么創(chuàng)建自定義starter,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)專(zhuān)注于蟠龍網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供蟠龍營(yíng)銷(xiāo)型網(wǎng)站建設(shè),蟠龍網(wǎng)站制作、蟠龍網(wǎng)頁(yè)設(shè)計(jì)、蟠龍網(wǎng)站官網(wǎng)定制、小程序定制開(kāi)發(fā)服務(wù),打造蟠龍網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供蟠龍網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。

前言:

Springboot的出現(xiàn)極大的簡(jiǎn)化了開(kāi)發(fā)人員的配置,而這之中的一大利器便是springboot的starter,starter是springboot的核心組成部分,springboot官方同時(shí)也為開(kāi)發(fā)人員封裝了各種各樣方便好用的starter模塊,例如:

  • spring-boot-starter-web//spring MVC相關(guān)

  • spring-boot-starter-aop //切面編程相關(guān)

  • spring-boot-starter-cache //緩存相關(guān)

starter的出現(xiàn)極大的幫助開(kāi)發(fā)者們從繁瑣的框架配置中解放出來(lái),從而更專(zhuān)注于業(yè)務(wù)代碼,而springboot能做的不僅僅停留于此,當(dāng)面對(duì)一些特殊的情況時(shí),我們可以使用我們自定義的springboot starter。
在創(chuàng)建我們自定義的starter之前呢,我們先看看官方是怎么說(shuō)的:

  • 模塊

在springboot官方文檔中,特別提到,我們需要?jiǎng)?chuàng)建兩個(gè)module ,其中一個(gè)是autoconfigure module  一個(gè)是 starter module ,其中 starter module 依賴(lài) autoconfigure module

但是,網(wǎng)上仍然有很多blog在說(shuō)這塊的時(shí)候其實(shí)會(huì)發(fā)現(xiàn)他們其實(shí)只用了一個(gè)module,這當(dāng)然并沒(méi)有錯(cuò),這點(diǎn)官方也有說(shuō)明:

You may combine the auto-configuration code and the dependency management in a single module if you do not need to separate those two concerns

//如果不需要將自動(dòng)配置代碼和依賴(lài)項(xiàng)管理分離開(kāi)來(lái),則可以將它們組合到一個(gè)模塊中。

  • 命名規(guī)范

springboot 官方建議springboot官方推出的starter 以spring-boot-starter-xxx的格式來(lái)命名,第三方開(kāi)發(fā)者自定義的starter則以xxxx-spring-boot-starter的規(guī)則來(lái)命名,事實(shí)上,很多開(kāi)發(fā)者在自定義starter的時(shí)候往往會(huì)忽略這個(gè)東西(因?yàn)椴豢垂俜轿臋n很難知道這件事情。同時(shí)也不會(huì)造成其他的后果,主要是顯得不夠?qū)I(yè))。

看看官方的starter

了解了這兩點(diǎn)之后,那么下面讓我們一塊去探索spingboot starter的奧秘吧。

按照springboot官方給的思路,starter的核心module應(yīng)該是autoconfigure,所以我們直接去看spring-boot-autoconfigure里面的內(nèi)容。

當(dāng)Spring Boot啟動(dòng)時(shí),它會(huì)在類(lèi)路徑中查找名為spring.factories的文件。該文件位于META-INF目錄中。打開(kāi)spring.factories文件,文件內(nèi)容太多了,為了避免我水篇幅,我們只看其中的一部分:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\

我們可以發(fā)現(xiàn)一些比較眼熟的單詞,比如Aop,Rabbit,Cache ,當(dāng)springboot啟動(dòng)的時(shí)候,將會(huì)嘗試加載這些配置類(lèi),如果該路徑下存在該類(lèi)的話,則將運(yùn)行它,并初始化與該配置類(lèi)相關(guān)的bean。

點(diǎn)開(kāi)一個(gè)看看:

@Configuration
@ConditionalOnClass({RabbitTemplate.class, Channel.class})
@EnableConfigurationProperties({RabbitProperties.class})
@Import({RabbitAnnotationDrivenConfiguration.class})
public class RabbitAutoConfiguration {
  
  //...代碼略..
}

我們先來(lái)了解一下這幾個(gè)注解:

@ConditionalOnClass :條件注解,當(dāng)classpath下發(fā)現(xiàn)該類(lèi)的情況下進(jìn)行自動(dòng)配置。

@EnableConfigurationProperties:外部化配置

@Import :引入其他的配置類(lèi)

當(dāng)然,這并不是一種通用的套路,查看其他的配置類(lèi),我們會(huì)發(fā)現(xiàn)其標(biāo)注的注解往往也是有所區(qū)別的。

自定義自己的starter

首先我們新建一個(gè)maven項(xiàng)目,引入以下依賴(lài):

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
  </dependencies>
  <dependencyManagement>
    <!-- 我們是基于Springboot的應(yīng)用 -->
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.1.0.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

然后我們創(chuàng)建一個(gè)person類(lèi),用作后期我們測(cè)試的bean

public class Person {

  //屬性
  private int age;
  private String name;
  private String gender;
  
  /*此處省略getter and setter and toStering*/

}

然后我們也創(chuàng)建一個(gè)PersonConfigProperties來(lái)完成我們屬性的注入

@ConfigurationProperties(prefix = "mystarter.config.student")
public class PersonConfigProperties {


  private String name;
  private int age;
  private String gender;

  /*
  其他的配置信息。。。。
   */
  
  /*此處省略getter and setter and toStering*/
}

最后創(chuàng)建我們的自動(dòng)配置類(lèi)MyStarterAutoConfiguration.java

@Configuration
@EnableConfigurationProperties(PersonConfigProperties.class)
@ConditionalOnClass(Person.class)
public class MyStarterAutoConfiguration {


  @Bean
  @ConditionalOnProperty(prefix = "mystarter.config", name = "enable", havingValue = "true")
  public Person defaultStudent(PersonConfigProperties personConfigProperties) {
    Person person = new Person();
    person.setAge(personConfigProperties.getAge());
    person.setName(personConfigProperties.getName());
    person.setGender(personConfigProperties.getGender());
    return person;
  }
}

我感覺(jué)這是不是做好了?

我不要你覺(jué)得,我要我覺(jué)得

最后我們最重要的一步:

在resourecs文件目錄下創(chuàng)建META-INF,并創(chuàng)建我們自己的spring.factories,并把我們的 MyStarterAutoConfiguration添加進(jìn)去

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jdkcb.mystarter.config.MyStarterAutoConfiguration

最后打包成jar包,在我們新的項(xiàng)目里面測(cè)試:

測(cè)試:

引入我們的starter,當(dāng)然也可以在本地直接引入我們的my-spring-boot-starter項(xiàng)目

  <dependency>
      <groupId>com.jdkcb</groupId>
      <artifactId>my-spring-boot-starter</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <scope>system</scope>
      <systemPath>${project.basedir}/src/main/resources/lib/my-spring-boot-starter-0.0.1-SNAPSHOT.jar</systemPath>
    </dependency>

在application.properties配置文件中添加我們相應(yīng)的配置

mystarter.config.enable=true
mystarter.config.person.name=小明
mystarter.config.person.age=5
mystarter.config.person.gender=男

新建一個(gè)測(cè)試的Controller:

@RestController
public class TestController {

  @Autowired
  private Person person;

  @RequestMapping("/getPerson")
  private Person getStudent() {
    return person;
  }

}

啟動(dòng)項(xiàng)目,在瀏覽器地址欄輸入 http://127.0.0.1:8080/getPerson ,結(jié)果如下

{"age":5,"name":"小明","gender":"男"}

以上是“Spring boot怎么創(chuàng)建自定義starter”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁(yè)標(biāo)題:Springboot怎么創(chuàng)建自定義starter
文章起源:http://jinyejixie.com/article2/podgoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)站改版、電子商務(wù)做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司
宿迁市| 沙湾县| 富平县| 周口市| 钦州市| 芒康县| 温泉县| 吴堡县| 周口市| 青州市| 延寿县| 佛坪县| 宁安市| 卓尼县| 绵竹市| 乌兰浩特市| 睢宁县| 资阳市| 准格尔旗| 永德县| 东阿县| 毕节市| 额济纳旗| 保山市| 渭南市| 顺平县| 聊城市| 泸水县| 类乌齐县| 瑞金市| 南澳县| 开远市| 吴川市| 无棣县| 张家港市| 镇平县| 广饶县| 饶河县| 太仓市| 探索| 阿克陶县|