網(wǎng)站首頁 編程語言 正文
目前創(chuàng)建一個(gè)自定義啟動(dòng)器:然后再SpringBoot中可以引入該依賴包,完成對(duì)該功能快速配置;
1.示意圖:?
?
2.實(shí)現(xiàn)步驟:
1.創(chuàng)建一個(gè)MAVEN項(xiàng)目,它定位于一個(gè)場景啟動(dòng)器:hello-spring-boot-starter,這個(gè)MAVEN項(xiàng)目只需要引入自定義的autoconfigurer依賴,其他src、resource都可以不用要;
<!-- 啟動(dòng)器 -->
<dependencies>
<!-- 引入自動(dòng)配置模塊 -->
<dependency>
<groupId>org.example</groupId>
<artifactId>hello-spring-boot-starter-autoconfigurer</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
2. 創(chuàng)建一個(gè)MAVEN項(xiàng)目,定義一個(gè)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定義一個(gè)屬性類:
package bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @program: hello-spring-boot-starter
* @description:
* @author: zdb
* @motto: 認(rèn)真寫好每一行代碼
* @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定義一個(gè)service類,該類默認(rèn)不放入IOC容器中,需要通過配置類完成對(duì)他的注入;
package service;
import bean.HelloProperties;
/**
* @program: hello-spring-boot-starter
* @description: 這個(gè)類通過HelloServiceAutoConfiguration實(shí)現(xiàn)注入到IOC容器中;
* @author: zdb
* @motto: 認(rèn)真寫好每一行代碼
* @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: 定義啟動(dòng)類starter的配置類;
* @author: zdb
* @motto: 認(rèn)真寫好每一行代碼
* @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文件,指明開啟自動(dòng)配置類;
# Auto Configure 配置啟動(dòng)類
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
config.HelloServiceAutoConfiguration
?3.測試
3.1創(chuàng)建一個(gè)springboot項(xiàng)目:hello-spring-boot-starter-test,創(chuàng)建一個(gè)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: 認(rèn)真寫好每一行代碼
* @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引入自定義啟動(dòng)器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創(chuàng)建application.properties文件:可以配置自定義啟動(dòng)器的屬性:
zdb.hello.prefix = hi
zdb.hello.suffix = ABCDEF****
3.4完成了,可以測試;
原文鏈接:https://blog.csdn.net/zdb292034/article/details/120258715
相關(guān)推薦
- 2022-04-23 通過CSS的sticky屬性 重新回顧 position
- 2024-07-15 解決`idea`中`database`工具查詢起別名亂碼問題
- 2021-10-12 shell實(shí)現(xiàn)Fisher–Yates?shuffle洗牌算法介紹_linux shell
- 2021-12-18 通俗易通講解Android藍(lán)牙鍵值適配_Android
- 2022-10-19 為什么不要在?Flutter?中使用全局變量_Android
- 2022-09-29 python繪制柱狀圖的方法_python
- 2022-12-21 React?中的重新渲染類組件及函數(shù)組件_React
- 2022-09-17 C++中cin>>n的返回值_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支