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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot 自定义starter 保姆级教程(说明+源码+配置+测试)

發布時間:2024/10/6 javascript 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot 自定义starter 保姆级教程(说明+源码+配置+测试) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.說明

命名歸約-官方命名:
前綴:spring-boot-starter-xxx(比如:spring-boot-starter-web)
命名歸約-自定義命名:
xxx-spring-boot-starter(比如:mybatis-spring-boot-starter)可憐的mybatis。

2.編寫啟動器

1、在IDEA中新建一個空項目 java-spring-boot-starter
2、新建一個普通Maven模塊:my-spring-boot-starter
3、新建一個Springboot模塊:my-spring-boot-starter-autoconfigure
4、點擊apply即可,基本結構
5、在 starter 中 導入 autoconfigure 的依賴

<!-- 啟動器 --><dependencies><dependency><groupId>com.example</groupId><artifactId>my-spring-boot-starter-autoconfigure</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>

6、將 autoconfigure 項目下多余的文件都刪掉,Pom中只留下一個 starter,這是所有的啟動器基本配置
7、編寫一個自己的服務

package com.example; public class HelloService {HelloProperties helloProperties;public HelloProperties getHelloProperties() {return helloProperties;}public void setHelloProperties(HelloProperties helloProperties) {this.helloProperties = helloProperties;}public String sayHello(String name){return helloProperties.getPrefix() + name + helloProperties.getSuffix();} }

8、編寫HelloProperties 配置類

package com.example; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "my.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;} }

9、編寫自動配置類并注入bean,測試

package com.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnWebApplication @EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration {@AutowiredHelloProperties helloProperties;@Beanpublic HelloService helloService(){HelloService service = new HelloService();service.setHelloProperties(helloProperties);return service;} }

10、在resources編寫一個自己的 META-INF\spring.factories

# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.kuang.HelloServiceAutoConfiguration

11、編寫完成后,可以安裝到maven倉庫中,看一下最終的結構:

3.測試

1、新建一個SpringBoot 項目
2、導入我們自己寫的啟動器

<dependency><groupId>org.example</groupId><artifactId>my-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency>

3、編寫一個 HelloController 進行測試我們自己的寫的接口!

package com.example.controller; import com.example.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController {@AutowiredHelloService helloService;@RequestMapping("/hello")public String hello() {return helloService.sayHello("-my-");} }

4、編寫配置文件 application.properties

my.hello.prefix="hello" my.hello.suffix="starter"

5、啟動項目進行測試,結果成功 !

總結

以上是生活随笔為你收集整理的SpringBoot 自定义starter 保姆级教程(说明+源码+配置+测试)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。