日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

springboot如何自定义starter

發(fā)布時(shí)間:2023/12/20 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot如何自定义starter 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.自定義啟動(dòng)器

lmh-hello-spring-boot-starter(啟動(dòng)器)
lmh-hello-spring-boot-starter-autoconfigure(自動(dòng)配置包)
1.1 創(chuàng)建一個(gè)空項(xiàng)目

1.2 在空項(xiàng)目的基礎(chǔ)上,添加maven空項(xiàng)目,項(xiàng)目名稱叫做
lmh-hello-spring-boot-starter
再添加一個(gè)springboot空項(xiàng)目,項(xiàng)目名稱lmh-hello-spring-boot-starter-autoconfigure

1.3 lmh-hello-spring-boot-starter(啟動(dòng)器)的pom.xml文件中,將lmh-hello-spring-boot-starter-autoconfigure的groupId和artifactId作為依賴添加進(jìn)去

1.4 注意每個(gè)類所在路徑
創(chuàng)建HelloService類,業(yè)務(wù)處理的過(guò)程

public class HelloService {@AutowiredHelloProperties helloProperties;/** param:用戶名* decription:再用戶名前后 加入前后綴* */public String sayHello(String userName){return helloProperties.getPrefix()+":"+userName+" : "+helloProperties.getSuffix();}}

創(chuàng)建HelloProperties類

@ConfigurationProperties("lmh.hello") //可用于便捷配置屬性值 public class HelloProperties {private String prefix;private String suffix;public String getPrefix() {return prefix;}public void setPrefix(String prefix) {this.prefix = prefix;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;} }

創(chuàng)建HelloServiceAutoConfiguration類

@Configuration @ConditionalOnMissingBean(HelloService.class) //當(dāng)容器中沒(méi)有HelloService組件時(shí),下面才生效 @EnableConfigurationProperties(HelloProperties.class) // 默認(rèn)放在容器中 public class HelloServiceAutoConfiguration {@Beanpublic HelloService helloService(){HelloService helloService = new HelloService();return helloService;}}

創(chuàng)建spring.factories

# Auto configure 啟動(dòng)時(shí),就開始加載 HelloServiceAutoConfiguration org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.auto.HelloServiceAutoConfiguration

1.5 將啟動(dòng)器和配置包 先clean,再install添加到maven倉(cāng)庫(kù)中![在這里
1.6 使用
在新項(xiàng)目中,添加啟動(dòng)器的依賴

<dependency><groupId>org.example</groupId><artifactId>lmh-hello-spring-boot0-starter</artifactId><version>1.0-SNAPSHOT</version> </dependency>

application.properties中

lmh.hello.prefix=username lmh.hello.suffix=77777

創(chuàng)建HelloController類

@RestController public class HelloController {@AutowiredHelloService helloService;@GetMapping("/hello")public String sayHello(){String s = helloService.sayHello("李白");return s;} }

1.7 地址訪問(wèn)
localhost:8080/hello

2.starter啟動(dòng)原理

  • pom.xml 添加starter,引入autoConfigure包
  • starter—> autoConfigure—>spring-boot-starter

  • autoconfigure包中配置使用META-INF/spring.factories 中EnableAutoConfiguration的值,使項(xiàng)目啟動(dòng)加載指定的自動(dòng)配置類

  • 編寫自動(dòng)配置類 xxxAutoConfiguration --> xxxProperties

    • @ConfigurationProperties(“l(fā)mh.hello”)
    • @Configuration
    • @ConditionalOnMissingBean(HelloService.class) //當(dāng)容器中沒(méi)有HelloService組件時(shí),下面才生效
    • @EnableConfigurationProperties(HelloProperties.class) // 默認(rèn)放在容器中
    • @Bean 組件
  • 總結(jié)

    以上是生活随笔為你收集整理的springboot如何自定义starter的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。