文章目录

  • 简介
  • 使用Spring Initializr创建一个项目
  • 定义一个配置信息映射类
  • 定义一个Service
  • 定义一个配置类自动装配Service
  • 在spring.factories中指定自动装配的类
  • 安装测试

简介

官方文档中关于自定义starter的描述

4.29.5. Creating Your Own Starter
Concretely, a custom starter can contain the following:
• The autoconfigure module that contains the auto-configuration code for “acme”.
• The starter module that provides a dependency to the autoconfigure module as well as “acme” and any additional dependencies that are typically useful. In a nutshell, adding the starter should provide everything needed to start using that library.

  • autoconfigure module 代码写在此moudle中
  • starter module只是一个空的项目,它的唯一目的是提供必要的依赖

如果配置简单,没有可选项,可以把所有东西都写在一个Moudle中

使用Spring Initializr创建一个项目

SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。

删掉一些不需要的东西,项目结构和pom如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.xgsama</groupId><artifactId>xg-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.1</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency></dependencies>
</project>

定义一个配置信息映射类

package com.xgsama.starter.autoconfigure;import org.springframework.boot.context.properties.ConfigurationProperties;
// 该注解有一个prefix属性,通过指定的前缀,绑定配置文件中的配置
@ConfigurationProperties("xg.hello")
public class HelloProperties {private String prefix;private String suffix;public String getPrefix() {return prefix;}public void setPrefix(String prefix) {this.prefix = prefix;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;}
}

定义一个Service

package com.xgsama.starter.service;import com.xgsama.starter.autoconfigure.HelloProperties;import javax.annotation.Resource;/*** HelloService** @author xgSama* @date 2021/1/10 22:59*/
public class HelloService {@ResourceHelloProperties properties;public String sayHello(String name) {return properties.getPrefix() + ":" + name + "!" + properties.getSuffix();}
}

定义一个配置类自动装配Service

@EnableConfigurationProperties使 @ConfigurationProperties 注解的类生效
@ConditionalOnMissingBean指定在某个Bean不存在时配置才生效

package com.xgsama.starter.autoconfigure;import com.xgsama.starter.service.HelloService;
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;/*** HelloServiceAutoConfiguration** @author xgSama* @date 2021/1/10 22:59*/
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {@Bean@ConditionalOnMissingBean(HelloService.class)public HelloService helloService() {return new HelloService();}
}

在spring.factories中指定自动装配的类

在classpath下创建META-INF,新建spring.factories文件

添加以下内容,指定上步骤中定义的配置类为自动装配的配置。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xgsama.starter.autoconfigure.HelloServiceAutoConfiguration

安装测试

执行maven clean install将自定义的starter安装到本地maven仓库,新建一个Spring Boot测试这个starter
引入依赖

<dependency><groupId>com.xgsama</groupId><artifactId>xg-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version>
</dependency>

添加配置

server:port: 8888
xg:hello:prefix: hellosuffix: nice to meet you

编写测试Controller

/*** AlphaController** @author xgSama* @date 2020/12/26 10:44*/
@RestController
public class AlphaController {@ResourceHelloService helloService;@GetMapping("/hello")public String hello() {String sayHello = helloService.sayHello("玄戈");return sayHello;}
}

启动项目测试

自定义一个SpringBoot Starter相关推荐

  1. 自定义一个springboot启动器

    对于springboot-starter很好理解即让spring容器帮我们自动配置引入的组件. 下面具体讲一下怎么创建自定义的启动器. 首先我们需要用idea新建一个空的项目 接下来new一个moud ...

  2. SpringSecurity Oauth2 - 自定义 SpringBoot Starter 远程访问受限资源

    文章目录 1. 自定义 SpringBoot Starter 1. 统一的dependency管理 2. 对外暴露 properties 3. 实现自动装配 4. 指定自动配置类的路径 META-IN ...

  3. Spring Boot学习总结(22)——如何定制自己的 springboot starter 组件呢?

    引言 我们日常项目中都会用到springboot,只要我们用到springboot,一定会用到各种spring-boot-starter.下面我们通过一个springboot starter 的dem ...

  4. SpringBoot 自定义实现一个启动器starter 教程。

    说明:springboot 官方给我们提供了很多启动器如:elasticsearch,aop,redis...等等 但是实际开发中,可能不同公司的业务不同需要定制化一个通用的专属的启动器来满足公司内部 ...

  5. 保姆级教程,手把手教你实现一个SpringBoot的starter

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

  6. springboot中下面哪一个作为jpa默认实现_天天在用SpringBoot,手撸一个的Starter试试!...

    引言 上篇文章<天天用SpringBoot,它的自动装配原理却说不出来>我们有说springBoot的自动装配怎么实现的,这篇文章的话我们就自己来实现一个SpringBoot的 start ...

  7. 如何自定义一个starter组件

    本文来说下如何自定义一个stater组件. 文章目录 概述 原理浅谈 术语介绍 starter组件命名规则 概述 我们都知道可以使用 SpringBoot 快速的开发基于 Spring 框架的项目.由 ...

  8. 简述SpringBoot Starter原理及自定义实现

    简述SpringBoot Starter原理及自定义实现 一.简述 二.结合SpringBoot启动原理看容器如何实现自动装配 三.解析mybatis-spring-boot-starter包看myb ...

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

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

最新文章

  1. 系统架构设计:平滑发布和ABTesting
  2. const在函数声明中的应用(转)
  3. C语言指针总结大学霸IT达人
  4. 回顾以前的线程安全的类
  5. tkinter 10 messagebox 弹窗
  6. [导入]ASP.NET MVC框架开发系列课程(3):URL导向.zip(16.66 MB)
  7. java编译找不到符号_javac编译时找不到符号?
  8. html---textarea初始化时就有个table空格以及tab键操作无效
  9. python的celery的面试_python面试基础题总结
  10. oracle——监听(三、监听配置)
  11. 徐州医科大学党委书记夏有兵一行莅临云创
  12. XenServer学习笔记1虚拟磁盘和虚拟内存
  13. 烧录软件:mcuisp和FlyMcu下载
  14. 纤维过滤器和石英砂过滤器的区别
  15. python期货基本面分析_Python量化炒期货入门与实战技巧
  16. win7开不了机按f8修复计算机没反应,win7开不了机按f8没用怎么办
  17. 如何成为一个 IT 界的女装大佬?
  18. python中合法的八进制数是_0o12f 是合法的八进制数字。 (2.0分)_学小易找答案
  19. java接口如何有效防止恶意请求
  20. vivo手机里的log是什么意思?

热门文章

  1. Jquery遍历数组之$.inArray()方法介绍
  2. 你知道怎样音频转文字吗?3个方法教你音频转文字怎么操作
  3. python二维数组切片举例
  4. 如何搜索添加大量高质量微信社群
  5. maya刷权重时有个叉_MAYA如何刷权重,求详细过程,当然,教程也可,
  6. access在sql中横向求和_使用查询对数据求和
  7. 我的世界服务器成就系统的其他成就是什么,《我的世界》盒子怎么玩 玩法成就系统介绍...
  8. java 视频添加音乐,如何给视频添加纯音乐作为配乐?视频配乐的图文教程分享...
  9. AI(Adobe Illustrator)简单入门——小熊
  10. Spring的IOC和AOP思想