springboot2.x basic tutorial: make a starter package by hand

The previous blog introduced the principle of springboot automatic assembly. Springboot itself has a wealth of spring-boot-starter-xx integrated components. In this article, we will use the springboot automatic assembly mechanism to make a starter package of our own.

Ideas for making a starter package

​ For this blog, I made a starter for uploading pictures to third-party image beds, integrating common third-party image beds sm.ms, imgur, github image beds, etc.

​ This tutorial will not specifically explain the code related to uploading to the bed, but mainly analyze the ideas of packaging this starter.

  1. First install the springboot third-party starter standard name: xx-spring-boot-starter, our project is named imghost-spring-boot-starter.
  2. For the configuration items related to the image bed, we are also going to create an ImgHostProperties configuration class to store.
  3. Similarly, we also need an ImgHostAutoConfiguration, and add conditional annotations to inject our tool class into the IOC container under certain circumstances.
  4. According to the specification, specify the auto-assembly class of our starter under the META-INF/spring.factories file of our project.

Project structure at a glance

image-20200911224040555

Starter development example

Introduce necessary dependencies

​ The spring-boot-starter package is mainly introduced here, and other dependencies of spring-boot-configuration-processor are mainly for uploading to a third-party image bed to send Http request dependency packages.

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

Define a configuration class for uploading image bed parameters

​ The token uploaded to the API of SM.MS needs to be uploaded. Register on the sm.ms website to obtain the personal private key. If you upload to imgur later, you can also add the corresponding configuration class to this class.

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

Define upload service AutoConfiguration class

When the imghost.smms.token user configures, we generate a SMMSImgHostService image bed upload service class.

@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);
	}
}

Write spring.factories

​ Finally, add META-INF/spring.factories to the project's src/main/resource to introduce our custom ImgHostAutoConfiguration configuration class.

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

how to use

  1. Introduce our imghost-spring-boot-starter into the project used.
<dependency>
	<groupId>vip.codehome</groupId>
	<artifactId>imghost-spring-boot-starter</artifactId>
	<version>1.0-SNAPSHOT</version>
</dependency>
  1. Add the following configuration to the springboot project
    image-20200911224308917

  2. Project use

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

to sum up

A thousand miles begins with one step. This is the eighteenth in the SpringBoot tutorial series. The above is the whole process of making a starter package by ourselves. Is it very simple? The source code of this project can be downloaded on github

Currently only achieved uploaded to SM.MS diagram bed, a late iteration will gradually be uploaded to sm.ms, imgur, github various figures beds generic tools, so stay tuned. If you think it’s good, like, comment, and follow three combos

Guess you like

Origin blog.csdn.net/github_35592621/article/details/108545152