SpringBoot系列之自定义starter实践教程
SpringBoot系列之自定义starter实践教程
Springboot是有提供了很多starter的,starter翻译过来可以理解为场景启动器,所谓场景启动器配置了自动配置等等对应业务模块的一个工程,有需要时候直接引入项目就可以,比如需要使用rabbitMQ,直接引入spring-boot-starter-activemq既可,详细介绍可以参考Springboot官方文档关于starters的介绍
查看官方文档,可以找到如下的命名规范:

其意思是SpringBoot官方的starter命名要定义为spring-boot-starter-*,自定义或者说第三方的要命名为thirdpartyproject-spring-boot-starter
- 官方命名空间
- 前缀:“spring-boot-starter-”
- 模式:spring-boot-starter-模块名
- 举例:spring-boot-starter-web、spring-boot-starter-actuator、spring-boot-starter-jdbc
- 自定义命名空间
- 后缀:“-spring-boot-starter”
- 模式:模块-spring-boot-starter
- 举例:mybatis-spring-boot-starter
ok,SpringBoot是可以自定义一些starter来使用的,可以用于方便项目开发,本博客以例子的方式介绍:
新建一个没有过多依赖的SpringBoot工程,spring-boot-starter必须引入,其它的可以Junit可以去掉,application类等等都不用,建议先新建一个Empty project,再引入对应配置的方式进行工程创建
pom.xml参考:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.springboot</groupId>
<artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>custom-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
模仿其它starter,新建Properties配置类
package com.example.springboot.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "custom.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;
}
}
编写业务测试类:
package com.example.springboot.starter.service;
import com.example.springboot.starter.HelloProperties;
public class HelloService {
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(String name){
return helloProperties.getPrefix()+"-"+name+helloProperties.getSuffix();
}
}
编写自定义的自动配置类:
package com.example.springboot.starter;
import com.example.springboot.starter.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* <pre>
* 自定义的自动配置类
* </pre>
*
* <pre>
* @author nicky.ma
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020/01/02 14:31 修改内容:
* </pre>
*/
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
必须新建META-INF/spring.factories,然后加入如下配置,自动配置类才可以被扫描到,具体原因可以参考我之前的源码学习笔记:Springboot源码学习专栏
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.springboot.starter.HelloServiceAutoConfiguration
example比较简单,starter就创建好了,接着需要新建一个web工程来实践,如图maven配置,引入custom-spring-boot-starter-autoconfigurer依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.example.springboot</groupId>
<artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
新建一个接口测试一下即可:
@Autowired
HelloService helloService;
@GetMapping(value = {"/sayHello/{name}"})
@ResponseBody
public String sayHello(@PathVariable("name")String name){
return helloService.sayHello(name);
}
运行项目后,返回链接即可:http://localhost:8082/web/sayHello/testname
例子代码下载:github下载链接
SpringBoot系列之自定义starter实践教程的更多相关文章
- SpringBoot系列之RabbitMQ使用实用教程
SpringBoot系列之RabbitMQ使用实用教程 @ 目录 1. 消息队列概述 1.1 MQ的概述 1.2 MQ目的地形式 2. 消息队列实现方式 2.1 常见MQ框架 2.2 MQ实现方式 3 ...
- SpringBoot系列之日志框架使用教程
目录 1.SpringBoot日志级别 1).日志级别简介 2).默认日志级别 3).配置日志级别 4).日志分组设置 2.SpringBoot日志格式设置 1).默认格式原理简介 2).默认日志格式 ...
- Spring-Boot自定义Starter实践
此文已由作者王慎为授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. disconf-spring-boot-starter 使用方法: 引入maven依赖: <depen ...
- SpringBoot系列之集成Dubbo示例教程
一.分布式基本理论 1.1.分布式基本定义 <分布式系统原理与范型>定义: "分布式系统是若干独立计算机的集合,这些计算机对于用户来说就像单个相关系统" 分布式系统(d ...
- SpringBoot 系列 - 自己写starter
原文地址: https://www.xncoding.com/2017/07/22/spring/sb-starter.html 前言: Spring Boot由众多Starter组成,随着版本的推移 ...
- SpringBoot学习(2) - 自定义starter
自己开发一个spring boot starter的步骤1.新建一个项目(全部都基于maven),比如新建一个spring-boot-starter-redis的maven项目 pom.xml: &l ...
- SpringBoot系列之快速创建项目教程
本博客简介一下SpringBoot快速创建工程的方法,主要介绍一下Spring Initializer,Spring Initializer是IntelliJ IDEA才集成的一种快速创建Spring ...
- SpringBoot使用AutoConfiguration自定义Starter
https://segmentfault.com/a/1190000011433487
- SpringBoot系列之从入门到精通系列教程
对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 Spring框架:作为JavaEE框架领域的一款重要的开源框架,在企业应用开发中有着很重要的作用,同时Spring框架及其子框架很多, ...
随机推荐
- 16-1 djanjo介绍
一 web框架的本质 1用户的浏览器(socket客户端) 和 网站的服务器(socket服务端)之间 2 HTTP协议: 1.1 请求(request) 1.2. 响应(response) 3 we ...
- 容器服务kubernetes federation v2实践五:多集群流量调度
概述 在federation v2多集群环境中,通过前面几篇文章的介绍,我们可以很容易的进行服务多集群部署,考虑到业务部署和容灾需要,我们通常需要调整服务在各个集群的流量分布.本文下面简单介绍如何在阿 ...
- [Pytorch]基于混和精度的模型加速
这篇博客是在pytorch中基于apex使用混合精度加速的一个偏工程的描述,原理层面的解释并不是这篇博客的目的,不过在参考部分提供了非常有价值的资料,可以进一步研究. 一个关键原则:“仅仅在权重更新的 ...
- 各种浏览器的兼容css
http://blog.csdn.net/wyx100/article/details/50450728 1.Mozilla内核[css]元素选择器{-moz-transition:运动的样式 持续时 ...
- jq杂项方法/工具方法----isArray()
https://www.cnblogs.com/sandraryan/ $.isArray()函数用于判断指定参数是否是一个数组.返回布尔值. <!DOCTYPE html> <ht ...
- tp5 thinkphp5 多表关联查询 join查询
model下: $res = \think\Db::name('article') ->alias("a") //取一个别名 ->join('admin ad','a. ...
- 你以为SSL是安全的吗?
在现代的IT安全领域,很大程度上依赖SSL来保障通讯安全.但SSL是安全的吗? 在2005年,王小云证明SHA-1能在较短的时间内找到碰撞.王小云发现SHA-1的安全弱点是偶然还是必然? 就我所知,各 ...
- Linux下FTP的安装和登陆
对于一个经常接触电脑的人来说,FTP无形中出现在我们生活的各个角落.日常生活中的文件上传和下载很多时候就是依靠FTP去实现的. 专业的说,FTP 是File Transfer Protocol(文件传 ...
- Redis - 命令行工具
使用Redis内置的命令行工具 redis-cli一些便捷的命令: 1.执行单条命令 平时在访问 Redis 服务器,一般都会使用 redis-cli 进入交互模式,然后一问一答来读写服务器,这种情况 ...
- linux自旋锁函数
我们已经看到 2 个函数, spin_lock 和 spin_unlock, 可以操作自旋锁. 有其他几个函 数, 然而, 有类似的名子和用途. 我们现在会展示全套. 这个讨论将带我们到一个我们无 法 ...