自定义starter的方法步骤

官方的给出的步骤是再在一个项目下创建两个模块,分别是spring-boot-starter模块和autoconfigure模块,autoconfigure主要进行自动配置,spring-boot-starter作用是依赖于autoconfigure模块,在导入spring-boot-starter模块后会自动导入autoconfigure模块,亦可以合并两个模块

项目结构(这里合并成了一个模块,效果是一样的)
该模块是使用spring Initailizr 创建的

package com.wocnz.mystarter;

import org.springframework.beans.factory.annotation.Autowired;

public class MYService {
   

    @Autowired
    MyServiceProperties myServiceProperties;



    public String getName(){
   
        return myServiceProperties.getName();
    }



}

MyServiceAutoConfiguration .java


package com.wocnz.mystarter;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.security.PublicKey;

@Configuration

@EnableConfigurationProperties(MyServiceProperties.class)
public class MyServiceAutoConfiguration {
   



    @Bean
    public  MYService myService(){
   
        return new MYService();
    }
}

MyServiceProperties.java


package com.wocnz.mystarter;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("myservice")
public class MyServiceProperties {
   
    private String name;

    public MyServiceProperties() {
   
    }

    public String getName() {
   
        return this.name;
    }

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

spring.factories
springboot在自动装配时,会获取文件信息,将MyServiceAutoConfiguration作为其中的一个自动配置类,进行装配,MyServiceAutoConfiguration类中配置了要注入容器的组件,可根据@Conditional注解判断是否将组件注入到容器中,从而实现"自动配置,按需开启"的功能.

# 
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wocnz.mystarter.MyServiceAutoConfiguration

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>
    <groupId>com.wocnz</groupId>
    <artifactId>mystarter-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mystarter</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>


                <configuration>
                    <skip>true</skip>
                    <mainClass>com.wocnz.mystarter.MystarterApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

在创建starter过程中遇到的问题!!!

由于在starter的pom文件中配置了spring-boot-maven-plugin插件,需要在configuration节点中配置

<skip>true</skip>

才能解决导入starter依赖后找不到包的问题。当然这个插件可以删掉

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>


                <configuration>
                    <skip>true</skip>
                    <mainClass>com.wocnz.mystarter.MystarterApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>