上一篇博客介绍了springboot自动装配的原理。springboot本身有丰富的spring-boot-starter-xx集成组件,这一篇趁热打铁加深理解,我们利用springboot自动装配的机制,从零开始制作一个属于自己的starter包。

制作一个starter包思路

​这一篇博客我制作一个上传图片第三方图床的starter,集成常见的第三方图床sm.ms、imgur、github图床等。

​本教程不会具体的讲解图床上传相关的代码,而是主要分析封装此starter的思路。

  1. 首先安装springboot第三方的starter规范命名:xx-spring-boot-starter,我们项目取名为imghost-spring-boot-starter。
  2. 对于图床相关的配置项,我们同样准备建立一个ImgHostProperties配置类存放。
  3. 同样我们也需要一个ImgHostAutoConfiguration,并且加上条件注解在某些情况下才会注入我们的工具类到IOC容器中。
  4. 按照规范在我们项目的META-INF/spring.factories文件下,指定我们starter的自动装配类。

项目结构一览

Starter开发实例

引入必要的依赖

​ 这里主要引入spring-boot-starter包,spring-boot-configuration-processor其他依赖主要为上传到第三方图床发送Http请求依赖包。

<?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>vip.codehome</groupId>
<artifactId>imghost-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/spring.factories</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!--把注释源码也打入基础包中-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

定义一个图床参数上传的配置类

​ 上传到SM.MS的API需要上传的token,在sm.ms网站注册获取个人的私钥,后面如果上传到imgur同样可以在此类中加入对应的配置类。

@Data
@ConfigurationProperties(prefix = "imghost")
public class ImgHostProperties {
SMMS smms;
@Data
public static class SMMS{
String token;
}
}

定义上传服务AutoConfiguration类

当imghost.smms.token使用者配置时,我们生成一个SMMSImgHostService的图床上传服务类。

@Configuration
@EnableConfigurationProperties(ImgHostProperties.class)
@ConditionalOnProperty(prefix = "imghost",name = "enabled",havingValue = "true",matchIfMissing = true)
public class ImgHostAutoConfiguration {
private final ImgHostProperties imgHostProperties; public ImgHostAutoConfiguration(ImgHostProperties imgHostProperties) {
this.imgHostProperties = imgHostProperties;
} @ConditionalOnMissingBean
@ConditionalOnProperty(prefix="imghost.smms",name="token")
@Bean
public SMMSImgHostService imgHostService() {
return new SMMSImgHostService(imgHostProperties);
}
}

编写spring.factories

​ 最后在项目的src/main/resource上加入META-INF/spring.factories中引入我们自定义的ImgHostAutoConfiguration配置类。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=vip.codehome.imghost.ImgHostAutoConfiguration

如何使用

  1. 在使用的项目中引入我们的imghost-spring-boot-starter。
<dependency>
<groupId>vip.codehome</groupId>
<artifactId>imghost-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
  1. 在springboot项目中加入如下配置

  2. 项目使用

	@Autowired
SMMSImgHostService smms;
public void upload() {
System.out.println(smms.upload(newFile("D:\\test.jpg")));
}

总结

千里之行,始于足下。这里是SpringBoot教程系列第十八篇。以上就是我们自己动手制作一个starter包的全过程,是不是很简单。此项目在github可下载源码

当前只是实现了上传到SM.MS图床,后期会逐渐迭代一个上传到sm.ms,imgur,github各种图床的通用工具类,敬请期待。如果觉得不错,点赞、评论、关注三连击

最新文章

  1. (转)PostgreSQL 兼容Oracle - orafce
  2. 关于变量和函数前&amp;符号的作用
  3. 【转】ASP.NET 高效分页存储过程
  4. 【Qt】Qt环境搭建(Visual Studio)【转】
  5. jquerymobile知识点:select的动态帮定
  6. opennebula extend(expending) auth module ldap
  7. ExtJS4 的dom
  8. Redis中的基本数据结构
  9. This is very likely to create a memory leak 异常
  10. mac os app 开发
  11. ES6常用语法(上)
  12. 火币网行情获取的websocket客户端
  13. kylin 系列(一)安装部署
  14. 【九天教您南方cass 9.1】01 安装Cad和Cass9.1
  15. 统计请求最高的TOP 5
  16. Prototype原型模式(创建型模式)
  17. go语言之进阶篇无缓冲channel
  18. 从头认识Spring-1.14 SpEl表达式(1)-简单介绍与嵌入值
  19. CI框架 -- 核心文件 之 Output.php(输出类文件)
  20. 关于凑数问题的dfs

热门文章

  1. Java—io流之打印流、 commons-IO
  2. Kruscal算法求图的最小生成树
  3. 运放引脚悬空危害大,单片机PWM信号进入运放需要考虑避免运放引脚悬空
  4. PMI-ACP认证,你了解多少?
  5. 虚拟化技术之kvm磁盘管理工具qemu-img
  6. Jmeter 常用函数(4)- 详解 __setProperty
  7. CSP-J2019 NOIP普及组初赛真题(阅读程序部分)
  8. composer安装包的时候触发PHP fatal error,提示允许的内存耗光
  9. Linux权限之/etc/passwd文件
  10. Play it again: reactivation of waking experience and memory