1、新建工程,在官方文档中命名规范为   xxx-spring-boot-starter   xxx为你的starter的项目功能名字

2、在pom.xml文件中加入两个依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>2.3.3.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><version>2.3.3.RELEASE</version>
</dependency>

3、目录结构如下

4、PersonProperties类

@ConfigurationProperties(prefix = "spring.person")
public class PersonProperties {// 姓名private String name;// 年龄private int age;// 性别private String sex = "M";// Getter & Setter

5、PersonService类

public class PersonService {private PersonProperties properties;public PersonService() {}public PersonService(PersonProperties properties) {this.properties = properties;}public void sayHello(){System.out.println("大家好,我叫: " + properties.getName() + ", 今年" + properties.getAge() + "岁"+ ", 性别: " + properties.getSex());}
}

6、PersonServiceAutoConfiguration类

首先需要明白下面一张图的配置内容

@EnableConfigurationProperties:外部化配置

@Configuration
@EnableConfigurationProperties(PersonProperties.class)
@ConditionalOnClass(PersonService.class)
@ConditionalOnProperty(prefix = "spring.person", value = "enabled", matchIfMissing = true)
public class PersonServiceAutoConfiguration {@Autowiredprivate PersonProperties properties;@Bean@ConditionalOnMissingBean(PersonService.class)  // 当容器中没有指定Bean的情况下,自动配置PersonService类public PersonService personService(){PersonService personService = new PersonService(properties);return personService;}
}

7、spring.factories文件

META-INF是自己手动创建的目录,spring.factories也是手动创建的文件,在该文件中配置自己的自动配置类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sf.hello.PersonServiceAutoConfiguration

8、最后将项目打包 mvn clean install  

maven会自动将包复制到你的maven仓库,在原始项目中pom文件开头

<groupId>hello</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>

所以你的包在hello文件夹中

9、在其他项目中引入jar包

<dependency><groupId>hello</groupId><artifactId>hello-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version>
</dependency>

10、在yml文件中编辑配置信息

spring:person:age: 28name: csysex: M

11、test类中测试

@SpringBootTest
class DemoApplicationTests {@Autowired@SuppressWarnings("ALL")private PersonService personService;@Testvoid contextLoads() {}@Testpublic void testHello(){personService.sayHello();}
}

到此结束!完美

spring-boot-starter 自定义相关推荐

  1. 快速开发一个自定义 Spring Boot Starter ,希望你也会

    来源:http://t.cn/Ai9li9fC 众所周知,Spring Boot由众多Starter组成,随着版本的推移Starter家族成员也与日俱增.在传统Maven项目中通常将一些层.组件拆分为 ...

  2. 自定义 Spring Boot Starter

    一.引言 什么是Spring Boot Starter呢?我们直接来看看官网是怎么介绍的吧. Starters are a set of convenient dependency descripto ...

  3. 自定义依赖注解无效_最详细的自定义Spring Boot Starter开发教程

    1.前言 随着Spring的日渐臃肿,为了简化配置.开箱即用.快速集成,Spring Boot 横空出世.目前已经成为 Java 目前最火热的框架了.平常我们用Spring Boot开发web应用.S ...

  4. Spring Boot:自定义starter

    来源:阿杜的世界 号外:最近整理了一下以前编写的一系列Spring Boot内容,整了个<Spring Boot基础教程>的PDF,关注我,回复:001,快来领取吧-!更多内容持续整理中, ...

  5. 手把手教你定制标准 Spring Boot starter

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 写在前面 我们每次构建一个 Spring 应用程序时,我 ...

  6. 一个项目有两个pom_实现一个Spring Boot Starter超简单,读 Starter 源码也不在话下...

    Spring Boot 对比 Spring MVC 最大的优点就是使用简单,约定大于配置.不会像之前用 Spring MVC 的时候,时不时被 xml 配置文件搞的晕头转向,冷不防还因为 xml 配置 ...

  7. 《SpringCloud超级入门》Spring Boot Starter的介绍及使用《七》

    目录 Spring Boot Starter项目创建 自动创建客户端 使用 Starter 使用注解开启 Starter 自动构建 使用配置开启 Starter 自动构建 配置 Starter 内容提 ...

  8. 实现一个 Spring Boot Starter 原来如此简单,读 Starter 源码也不在话下

    我是风筝,公众号「古时的风筝」,一个在程序圈混迹多年,主业 Java,另外 Python.React 也玩儿的 6 的斜杠开发者.现已转行程序员鼓励师 Spring Cloud 系列文章已经完成,可以 ...

  9. 一个简易上手的短信服务Spring Boot Starter,连傻瓜都会!

    作 者:jackieonway 来 源:jianshu.com/u/36510c75d37c 短信服务在用户注册.登录.找回密码等相关操作中,可以让用户使用更加便捷,越来越多的公司都采用短信验证的方式 ...

  10. Spring boot starter

    1:Spring boot starter及项目中的类似运用 1:Spring boot starter的两种方式 引入pom文件,自动管理jar版本 根据spring.factories配置文件,加 ...

最新文章

  1. SLAM学习,小白入门到殿堂级大牛资料整理
  2. 单元测试instrumentation入门---eclipse
  3. 奔小康赚大钱 hdu 2255( KM )
  4. Js的Url中传递中文参数乱码的解决
  5. 扫地机器人滤网顺序_扫地机器人如何维护 扫地机器人维护技巧【介绍】
  6. 你怎么看当前中文语音识别技术在国内的应用?
  7. 姆巴佩独造三球一战成名 阿里云打破世界杯流量纪录 1
  8. JavaScript开发规范要求
  9. 将table导出为Excel的标准无乱码写法
  10. 第08章节-Python3.5-Django工程创建 7
  11. tableexport 文件格式和扩展名不匹配_让信息检索更有效率!百度有哪些你不知道的隐藏玩法?...
  12. POS58 票据热敏打印机,怎么用ESC/POS命令控制打印文字大小?
  13. aistudio 常规赛:钢铁缺陷检测挑战赛 经验总结,轻松复现map 47排名再度提升
  14. python kivy教程,Python Kivy 中文教程:安装(Windows)
  15. js:Cannot use import statement outside a module
  16. 双系统重装win10后恢复grub引导
  17. 对接转账到支付宝账户API接口流程步骤
  18. Javascript实现扫雷游戏
  19. linux下安装nginx出错,Ubuntu安装Nginx服务器出错解决
  20. python实现B站Bv号转Av号

热门文章

  1. 华硕飞行堡垒FX53VD拆机详细教程
  2. 无限宝电脑连接不上服务器,无限宝客户端登录过程问题
  3. Java实现多条相同数据合并为一条数据
  4. 解决Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com
  5. 世界上最难的视觉图_看看世界上驾照最难考的国家,你还会觉得考驾照难吗?...
  6. Drupal中的Pathauto介绍(来自站长百科)
  7. 计算机组成原理(哈工大)学习笔记
  8. sys.stderr.write(f“ERROR: {exc}“) ^ SyntaxError: invalid syntax 问题
  9. 连接校园网的路由器为啥老是服务器没响应,校园网路由器不能使用怎么办?
  10. [嵌入式框架][nrf51822][SDK12.3] BLE分层设计 DFU OTA 透传(NUS) 电量 设备信息 BLE_HID