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

springboot自定义starter(避坑教学)

在实际开发中,经常会定义一些公共组件,提供给各个项目团队使用。而在springboot的项目中,一般会将这些公共组件封装为springboot的starter。

1.命名规范

Spring官方Starter通常命名为 spring-boot-starter-{name}
        如:spring-boot-starter-web
Spring官方建议非官方Starter命名应遵循 {name}-spring-boot-starter的格式:
        如 mybatis-spring-boot-starter。

2.首先创建一个SpringBoot基础工程

注意生成启动类pom坐标可以如图位置修改

pom文件如下

  <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies>

3.创建自动配置类

工程下创建config包,创建UserAutoConfiguratio类

UserAutoConfiguratio代码:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@ConditionalOnClass(UserService.class)
@EnableConfigurationProperties(UserProperties.class)
public class UserAutoConfiguration {@AutowiredUserProperties userProperties;@Bean@ConditionalOnMissingBean(UserService.class)public UserService userService(){//将userService的字段赋值并注入到ioc容器return new UserService(userProperties.getUserName(), userProperties.getHobby());}
}

这个类作用是读取application.yml的配置属性

4.创建配置类:UserProperties

继续在config包下,创建UserAutoConfiguratio类

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;/*** @author qinbo* @version 1.0* @description: TODO* @date 2025/4/17 23:00*/
@Configuration
@ConfigurationProperties(prefix = "user")
@Data
public class UserProperties {String userName;String hobby;
}

这个类作用是接收yml文件的属性,得到映射

注意yml的写法

user:username: adminhobby: 123456

5.业务类:UserService

继续在config包下,创建UserService类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserService {String name;String hobby;public String test(){System.out.println("name"+name+"\t\thobby"+hobby);return "this is test method";}
}

这个类就是处理业务

6.创建META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件

在resources目录下创建

成功后如下图

这个地方博主掉坑里了/(ㄒoㄒ)/~~

里面编写自动配置类的路径(例如)

com.bo.userstatetr.config.UserAutoConfiguration

最后就是通过clear  install了,把jar包打到本地仓库

现在starter已经打包完成,那么我们如何在另一个boot项目中使用他

(1)导入坐标依赖

这就是之前项目的pom文件

在另一个项目中yml进行配置属性

最后就是依赖注入进行调用

import com.bo.userstatetr.config.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class ExceldemoApplicationTests {@AutowiredUserService userService;@Testvoid contextLoads() {userService.test();}}

相关文章:

  • OpenCV day5
  • kubernetes-高频率使用命令
  • LeetCode 2176.统计数组中相等且可以被整除的数对:两层遍历模拟
  • 【专题刷题】双指针(三):两数之和,三数之和,四数之和
  • 第八章:探索新兴趋势:Agent 框架、产品与开源力量
  • 新能源汽车能量流测试的传感器融合技术应用指南
  • .net C# 使用Epplus库将Datatable导出到Excel合并首列
  • Python 让课堂“沉浸式进化”——虚拟现实教学辅助工具的开发实战
  • Windows平台用vistual studio 2017打包制作C++动态库
  • STL详解 - stack与queue的模拟实现
  • 《AI大模型应知应会100篇》第22篇:系统提示词(System Prompt)设计与优化
  • USART讲解
  • 深入理解类:ArkTS面向对象编程的核心概念
  • 批量操作的优点
  • idea mvn执行打包命令后控制台乱码
  • 【无标题】STM32CubeMX
  • 【SpringBoot+Vue自学笔记】003 SpringBoot Controll
  • Oracle DBMS_SCHEDULER 与 DBMS_JOB 的对比
  • 【音视频开发】第五章 FFmpeg基础
  • k8s调度器:如何控制Pod的分布
  • 精品消费“精”在哪?多在体验上下功夫
  • 海昏侯博物馆展览上新,“西汉帝陵文化展”将持续展出3个月
  • 刘小涛任江苏省委副书记
  • 打击网络侵权盗版!四部门联合启动“剑网2025”专项行动
  • 德州国资欲退出三东筑工,后者大股东系当地房企东海集团
  • 小米汽车机盖门陷谈判僵局,车主代表称小米表示“退订会造成崩塌”