網站首頁 編程語言 正文
目前創建一個自定義啟動器:然后再SpringBoot中可以引入該依賴包,完成對該功能快速配置;
1.示意圖:?
?
2.實現步驟:
1.創建一個MAVEN項目,它定位于一個場景啟動器:hello-spring-boot-starter,這個MAVEN項目只需要引入自定義的autoconfigurer依賴,其他src、resource都可以不用要;
<!-- 啟動器 -->
<dependencies>
<!-- 引入自動配置模塊 -->
<dependency>
<groupId>org.example</groupId>
<artifactId>hello-spring-boot-starter-autoconfigurer</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
2. 創建一個MAVEN項目,定義一個hello-spring-boot-starter-autoconfigurer,并引入spring-boot-starter依賴;
<dependencies>
<!-- 引入spring-boot-starter,所有starter的基本配合 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-configuration-processor </artifactId>
<optional> true </optional>
</dependency>
</dependencies>
2.1定義一個屬性類:
package bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @program: hello-spring-boot-starter
* @description:
* @author: zdb
* @motto: 認真寫好每一行代碼
* @create: 2021-09-11 16:25
*/
//定義配置文件中屬性的前綴
@ConfigurationProperties(prefix = "zdb.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;
}
}
2.2定義一個service類,該類默認不放入IOC容器中,需要通過配置類完成對他的注入;
package service;
import bean.HelloProperties;
/**
* @program: hello-spring-boot-starter
* @description: 這個類通過HelloServiceAutoConfiguration實現注入到IOC容器中;
* @author: zdb
* @motto: 認真寫好每一行代碼
* @create: 2021-09-11 16:55
*/
public class HelloService {
/**
* 這里采用set方式注入屬性;
*/
private 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();
}
}
2.3定義配置類:
package config;
import bean.HelloProperties;
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;
import service.HelloService;
/**
* @program: hello-spring-boot-starter
* @description: 定義啟動類starter的配置類;
* @author: zdb
* @motto: 認真寫好每一行代碼
* @create: 2021-09-11 16:58
*/
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
private HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
?2.4在resources/META-INF定義spring.factories文件,指明開啟自動配置類;
# Auto Configure 配置啟動類
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
config.HelloServiceAutoConfiguration
?3.測試
3.1創建一個springboot項目:hello-spring-boot-starter-test,創建一個HelloController類:
package com.zhangdb.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import service.HelloService;
/**
* @program: hello-spring-boot-starter
* @description:
* @author: zdb
* @motto: 認真寫好每一行代碼
* @create: 2021-09-11 18:12
*/
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("hello/{name}")
public String hello(@PathVariable(value = "name") String name){
return helloService.sayHello(name);
}
}
3.2引入自定義啟動器starter:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入自定義的start包-->
<dependency>
<groupId>org.example</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>
3.3在resources創建application.properties文件:可以配置自定義啟動器的屬性:
zdb.hello.prefix = hi
zdb.hello.suffix = ABCDEF****
3.4完成了,可以測試;
原文鏈接:https://blog.csdn.net/zdb292034/article/details/120258715
相關推薦
- 2024-03-08 SpringBoot開發中VO、DTO的作用,以及使用場景
- 2023-01-12 C#實現讀寫ini配置文件的方法詳解_C#教程
- 2022-09-24 C#/VB.NET實現PPT或PPTX轉換為圖像功能_C#教程
- 2022-02-25 C++的靜態成員變量和靜態成員函數詳解_C 語言
- 2023-03-11 C/C++?-?從代碼到可執行程序的過程詳解_C 語言
- 2022-08-15 apollo配置中心的client端分析
- 2024-03-19 關于maven打包時,沒有將依賴包打進來的問題
- 2023-02-01 C#模擬實現QQ窗體功能_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支