Those of you who have used SpringBoot know that SpringBoot greatly simplifies the initial construction and development process of the project by default configuration. We just need to introduce the starter dependency in Maven, and SpringBoot can automatically scan the information to be loaded and start the corresponding default configuration. Starter lets us get rid of all the dependency libraries and simplify configuration.

What does a custom starter do

Since starter has such great advantages, can we use it in our daily development work? The answer is yes! We often have some configuration modules that are independent of the business. When we use them, we will put them into a specific package, or integrate them into a JAR package. Then, if another project needs to reuse this function, we need to copy the code to another project and integrate it again. If we package these power configuration modules that can be independent of the business code into a starter, we only need to reference the dependency in the POM when reuse.

How do I customize a starter

How can I start to customize a boot starter? Let’s make one with me.

First, notice the naming conventions:

The official package name of SpringBoot:

Prefix: spring-boot-starter- (module name)

Eg: spring-boot-starter-web, spring-boot-starter- AOP

You are advised to customize the starter name:

Suffix :(module name) -spring-boot-starter

Eg: mybatis – spring – the boot – the starter

This differentiates the official starter from the custom starter.

The introduction of pom

Create a New Maven project. Here I have a customized oss file upload starter, so name it: OSs-spring-boot-starter. Then add the POM dependency:

<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
Copy the code

In this way, the starter package can automatically obtain the configuration parameters of the project from the configuration file, thus flexibly adapting to project customization.

Defining configuration classes

Write a separate configuration class for the starter, which can be dynamically configured in a YAML file when the starter is introduced on the client:

@ConfigurationProperties(prefix = "oss")
@Data
@NoArgsConstructor
public class OssProperties {
    private String accessKey;
    private String secretKey;
    private String bucketName;
    private String prefixDomain;
    private String temporaryFolder;
    private String rootPath;
}
Copy the code

Define the service

Realize file upload, delete, move and other functions

public class OssFileService {
    private OssProperties ossProperties;
    public MinIoFileServiceImpl(OssProperties ossProperties) {
        this.ossProperties = ossProperties;
    }
    public void uploadFile(File file){}public void deleteFile(String fileName){}public void moveOrRenameFile(String oldName,String newName, String toBucket){}}Copy the code

Define program entry

@EnableConfigurationProperties(OssProperties.class)
@ConditionalOnProperty(prefix = "oss", name = "type", havingValue = "minio")
@Configuration
public class MinIoConfig {
    @Autowired
    private OssProperties ossProperties;

    @Bean
    public OssFileService fileService(a){
        return newMinIoFileServiceImpl(ossProperties); }}Copy the code

ConditionalOnProperty (@conditionAlonProperty); the bean will be automatically assembled if type=minio is set to condition.ossFileservice (@autoWired) can be used directly.

Configuration and package

“> < resources > < META-INF >”

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.lingxiao.oss.MinIoConfig
Copy the code

use

Introducing poM at the user end:

<dependency>
		<groupId>com.lingxiao</groupId>
		<artifactId>oss-spring-boot-starter</artifactId>
		<version>0.0.1 - the SNAPSHOT</version>
</dependency>
Copy the code

Configuration file application.yml:

oss:
  type: minio
  accessKey: admin 
  secretKey: 12345678
  bucketName: file  
  prefixDomain: http://oss.lingxiao.com
  rootPath: 
Copy the code

Then use @autoWired injection to do this:

@Autowired
private OssFileService ossFileService;
Copy the code

Well, isn’t that easy, kid? Did you get it?