编写spring boot starter

254 阅读1分钟

平时在是使用spring boot进行开发的时候,我们会用到很多starter,比如redis-starter,mongodb-starter,那么我们自己可以开发自己的starter吗?接下来我们来开发我们自己的第一个start!

my-starter

首先我们先要创建一个配置类StartProperties

//application.yml的配置信息以custom-start为前缀
@ConfigurationProperties("custom-start")
public class StartProperties {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

注册到bean工厂的service类 StartService

public class StartService {

    private StartProperties startProperties;

    public StartService(StartProperties startProperties) {
        this.startProperties = startProperties;
    }
    
    public void printProperties(){
        System.out.println(this.startProperties);
    }
}

自动配置类 StartAutoConfigure

@Configuration
@EnableConfigurationProperties(StartProperties.class)
public class StartAutoConfigure {

    private StartProperties startProperties;

    @Autowired
    public StartAutoConfigure(StartProperties startProperties1) {
        this.startProperties = startProperties1;
    }

    @Bean
    public StartService getStartService() {
        return new StartService(startProperties);
    }
}

在resources/META-INF/下创建spring.factories

#StartAutoConfigure上面我们的自配置类,spring根据spring.factories文件寻找自动配置类完成注册
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mystarter.chiv.autoConfiguration.StartAutoConfigure

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.3.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.my-starter</groupId>
    <artifactId>chiv</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>chiv</name>
    <description>starter</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </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-autoconfigure</artifactId>
        </dependency>
    </dependencies>



</project>

通过mvn install生成jar包,然后我们在另外一个项目中引用就可以了。

                 <dependency>
			<groupId>com.my-starter</groupId>
			<artifactId>chiv</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

配置文件添加

#完成配置信息的自动注入
custom-start:
  name: 111

那么 StartService,跟我们配置自己写的bean一样可以调用了。

总结

spring boot starter的编写主要的在于自动配置这一块,只要了解了spring是如何完成类的自己注册,我们就很轻松的编写starter了。

spring启动时扫描classpath下面的所有spring.factories,解析spring.factories获取所有EnableAutoConfiguration类,然后完成自动装配。