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

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 {

    @Resource
    private 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 {

    @Bean
    public 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#
    - Vue
  data-map:
    name: LiuPing
    age: 30
    address: HeNanXinXian

@Service
public class TestService {

    @Autowired
    private TestConfigurationService testConfigurationService;

    @Autowired
    private TestImportService  testImportService;

    public void  test(){
        testConfigurationService.showList();
        testImportService.showMap();
    }


http://www.kler.cn/a/400005.html

相关文章:

  • ubuntu显示管理器_显示导航栏
  • RK3568平台开发系列讲解(platform虚拟总线驱动篇)实验:点亮一个LED
  • .NET 简介
  • python读写excel等数据文件方法汇总
  • DQN系列算法详解
  • PyTorch实战-手写数字识别-单层感知机
  • PHP 伪静态详解及实现方法
  • java如何利用流式计算筛选出同一天时间最新的一条数据
  • 深度学习:卷积神经网络的计算复杂度,顺序操作,最大路径长度
  • 【鸿蒙开发】第十一章 Stage模型应用组件-任务Mission
  • MTU-内核态(数据链路层或网络接口上能够传输的最大数据包大小)
  • uniapp开发微信小程序笔记3-全局配置、导航栏配置、tabBar配置
  • [N1CTF 2018]eating_cms
  • uni-app项目搭建(ts+Vue3+pinia+vite)
  • StructuredStreaming (二)——kafka
  • Docker: ubuntu系统下Docker的安装
  • 免费开源!DBdoctor推出开源版系统诊断工具systool
  • Android屏幕横竖屏切换和生命周期
  • Hadoop 3.x 新特性详解
  • 【数据分享】中国渔业统计年鉴(1979-2024) pdf
  • GaussDB性能调优
  • 机器学习—偏差或方差与神经网络
  • 基于 AI 智能名片 2 + 1 链动模式商城小程序的立体连接营销策略研究
  • 问:Spring MVC DispatcherServlet流程步骤梳理
  • go 学习网站,go例子 go demo go学习视频
  • 基于Canny边缘检测和轮廓检测