springboot2.x基础教程:动手制作一个starter包

制作一个starter包思路#

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

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

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

项目结构一览#

image-20200911224040555
Starter开发实例#
引入必要的依赖#

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

Copy

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

<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>

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

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

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

定义上传服务AutoConfiguration类#

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

Copy
@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配置类。

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

如何使用#

在使用的项目中引入我们的imghost-spring-boot-starter。

Copy

vip.codehome
imghost-spring-boot-starter
1.0-SNAPSHOT

在springboot项目中加入如下配置
image-20200911224308917

项目使用

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值