当前位置: 首页 > news >正文

Spring Boot3自定义starter

1、加入必要依赖

plugins {id 'java'id 'org.springframework.boot' version '3.2.6'id 'io.spring.dependency-management' version '1.1.5'
}
group 'org.example.test.starter'
version '1.1.0'jar{enabled=true//  resolveMainClassName
}java {toolchain {languageVersion = JavaLanguageVersion.of(17)}
}repositories {maven { url 'https://maven.aliyun.com/repository/public/' }mavenCentral()
}dependencies {implementation 'org.springframework.boot:spring-boot-starter'implementation 'org.springframework.boot:spring-boot-configuration-processor'
}

2、定义Properties及业务service

@ConfigurationProperties(prefix = "test")
public class TestProperties {private List<String>  dataList;private Map<String,String>  dataMap;public List<String> getDataList() {return dataList;}public void setDataList(List<String> dataList) {this.dataList = dataList;}public Map<String, String> getDataMap() {return dataMap;}public void setDataMap(Map<String, String> dataMap) {this.dataMap = dataMap;}
}
public class TestConfigurationService {private List<String>  list;public void showList(){list.forEach(System.out::println);}public void setList(List<String> list) {this.list = list;}
}
public class TestConfigurationService {private List<String>  list;public void showList(){list.forEach(System.out::println);}public void setList(List<String> list) {this.list = list;}
}
@Component
public class TestImportService {@Resourceprivate TestProperties testProperties;public void  showMap(){Map<String, String> dataMap = testProperties.getDataMap();if(!CollectionUtils.isEmpty(dataMap)){Set<String> keySet = dataMap.keySet();keySet.forEach(key-> System.out.println("key="+key+"--value="+dataMap.get(key)));}}}

3、自动配置类

@EnableConfigurationProperties({TestProperties.class})
@Import({TestImportService.class})
@Configuration
public class TestStarterAutoConfiguration {@Beanpublic TestConfigurationService testConfigurationService(TestProperties testProperties){TestConfigurationService testConfigurationService = new TestConfigurationService();testConfigurationService.setList(testProperties.getDataList());return testConfigurationService;}}

         配置自动导入类,在resource创建META-INF/spring目录,在目录下创建org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,写入TestStarterAutoConfiguration 全路径

4、调用测试

test:data-list:- Java- C/C++- C#- Vuedata-map:name: LiuPingage: 30address: HeNanXinXian

@Service
public class TestService {@Autowiredprivate TestConfigurationService testConfigurationService;@Autowiredprivate TestImportService  testImportService;public void  test(){testConfigurationService.showList();testImportService.showMap();}

http://www.lryc.cn/news/486968.html

相关文章:

  • 掌控 Solidity:事件日志、继承和接口的深度解析
  • 新手教学系列——善用 VSCode 工作区,让开发更高效
  • Vue3 虚拟列表组件库 virtual-list-vue3 的使用
  • WebRTC实现双端音视频聊天(Vue3 + SpringBoot)
  • 第6章详细设计-6.9 PCB审查
  • docker与大模型(口语化原理和实操讲解)
  • Linux之vim模式下全选命令
  • 云原生周刊:Kubernetes v1.32 要来了
  • # JVM学习
  • 【代码随想录day33】【C++复健】62.不同路径;63. 不同路径 II;343. 整数拆分;96.不同的二叉搜索树
  • 《勇者斗恶龙3:HD-2D重制版》找幽灵船攻略分享
  • 基于 MATLAB 的模拟退火算法详解及实现
  • MQTT 服务器常用的有哪些?
  • 【android USB 串口通信助手】stm32 源码demo 单片机与手机通信 Android studio 20241118
  • 汽车资讯新探索:Spring Boot技术引领
  • 简单的MCU与FPGA通过APB总线实现通讯(fpga mcu APB):乘法器为例
  • css uniapp背景图宽度固定高度自适应可以重复
  • 深度学习--优化器
  • 【嵌入式】关于push老仓库到新仓库的方法
  • 从线下到线上,上门洗衣服务如何实现智能化升级?
  • SQL字段来源表的解析
  • 理解 Python 解释器:CPython 与 IPython 的比较及选择指南
  • Java NIO 深度解析:构建高效的 I/O 操作
  • 总结拓展十六:特殊采购业务——VMI采购模式
  • vue2 + iview(view-design) 中封装使用 vxe-table 处理表格渲染大量数据卡顿现象
  • 初学者指南:知识库问答(KBQA)多跳路径的核心与应用
  • 创建springboot+vue项目相关配置问题
  • 基于AOA算术优化的KNN数据聚类算法matlab仿真
  • 【机器学习】在泊松分布中,当λ值较大时,其近似正态分布的误差如何评估?
  • ABAP开发-面向对象开发_2