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

自定义Spring Boot Starter

自定义Spring Boot Starter

    • 1.引入依赖
    • 2.配置属性类
    • 3.业务服务类
    • 4.创建自动配置类
    • 5.注册自动配置
    • 6.案例使用
      • 6.1 引入依赖
      • 6.2 手动引入jar包
      • 6.3 yml中配置
      • 6.4 实际使用
    • 7.包结构

1.引入依赖

 <packaging>jar</packaging><dependencies><!-- Spring Boot 自动配置依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>2.6.13</version></dependency><!-- 可选:配置注解处理器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><version>2.6.13</version><optional>true</optional></dependency></dependencies>

2.配置属性类

package top.remained.lx.properties;import org.springframework.boot.context.properties.ConfigurationProperties;/*** @author lx* @date 2025/5/8* @description 配置属性类*/
@ConfigurationProperties(prefix = "lx")
public class LxProperties {private String name = "default";private int timeout = 1000;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getTimeout() {return timeout;}public void setTimeout(int timeout) {this.timeout = timeout;}
}

3.业务服务类

package top.remained.lx.service;/*** @author lx* @date 2025/5/8* @description 业务服务类*/
public class LxService {private final String name;private final int timeout;public LxService(String name, int timeout) {this.name = name;this.timeout = timeout;}public String greet() {return "Hello, " + name + "! (timeout: " + timeout + "ms)";}
}

4.创建自动配置类

package top.remained.lx.configuration;import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.remained.lx.properties.LxProperties;
import top.remained.lx.service.LxService;/*** @author lx* @date 2025/5/8* @description*/
@Configuration
@EnableConfigurationProperties(LxProperties.class)
@ConditionalOnClass(LxService.class)
@ConditionalOnProperty(prefix = "lx", name = "enabled",havingValue = "true",matchIfMissing = true)
public class LxConfiguration {@Bean
//    @ConditionalOnMissingBean(LxProperties.class) // 如果没有创建bean 则创建默认beanpublic LxService lxService(LxProperties lxProperties) {return new LxService(lxProperties.getName(), lxProperties.getTimeout());}
}

5.注册自动配置

在resources/META-INF下创建spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=top.remained.lx.configuration.LxConfiguration

6.案例使用

6.1 引入依赖

      <dependency><groupId>top.remained</groupId><artifactId>lx-stater</artifactId><version>2.7.8</version></dependency>

6.2 手动引入jar包

mvn install:install-file -D"file=C:\soft\fastexcel-1.1.0.jar" -D"groupId=cn.idev.excel" -D"artifactId=fastexcel" -D"version=1.1.0" -Dpackaging=jar

6.3 yml中配置

在这里插入图片描述

6.4 实际使用

@RequestMapping("/lx")
@RestController
public class TestLXStarterController {@Autowiredprivate LxService lxService;@GetMapping("/greet")public String greet() {return lxService.greet();}
}

7.包结构

在这里插入图片描述

http://www.xdnf.cn/news/749611.html

相关文章:

  • 【课堂笔记】标签传播算法Label Propagation Algorithm(LPA)
  • DFS入门刷题
  • vasp的输出文件解读--OUTCAR
  • 常见的RAG文档解析辅助工具汇总及企业选型思考
  • 一周学会Pandas2之Python数据处理与分析-数据重塑与透视-pivot() - 透视 (长 -> 宽,有限制)
  • [SC]SystemC在CPU/GPU验证中的应用(四)
  • 图像修复的可视化demo代码
  • PostIn入门教程 - 使用IDEA插件快速生成API接口定义
  • 流媒体基础分析:延迟分析与安全性保障
  • 牛客小白月赛117
  • Baklib知识中台驱动服务升级
  • OD 算法题 B卷【模拟消息队列】
  • Linux环境搭建MCU开发环境
  • [001]从操作系统层面看锁的逻辑
  • 计算机组织原理第三章
  • LearnOpenGL-笔记-其十二
  • 《高等数学》(同济大学·第7版) 的 详细章节目录
  • 顺序查找与折半查找
  • [python]Prophet‘ object has no attribute ‘stan_backend‘解决方法
  • Lyra学习笔记 Experience流程梳理
  • 5月31日day41打卡
  • 【Unity笔记】Unity WASD+QE 控制角色移动与转向(含 Shift 加速)实现教程
  • Qt -使用OpenCV得到SDF
  • thinkpad T-440p 2025.05.31
  • YOLOv10速度提升与参数缩减解析2025.5.31
  • 华为OD机试_2025 B卷_静态扫描(Python,100分)(附详细解题思路)
  • SAR ADC 同步逻辑设计
  • 【CBAP50技术手册】#31 Observation(观察法):BA(业务分析师)的“现场侦探术”
  • Kerberos面试内容整理-Kerberos 与 LDAP/Active Directory 的集成
  • 关于TongWeb数据源兼容mysql驱动的注意事项