自己写一个spring boot starter


(1) 新建项目
项目名:formatter-spring-boot-starter

(2) 定义pom.xml

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

    
        first-app-by-gui
        thinking-in-spring-boot
        0.0.1-SNAPSHOT
    
    4.0.0

    formatter-spring-boot-starter

    
        
        
            org.springframework.boot
            spring-boot-starter
            2.3.4.RELEASE

            true
        

    

(3) 写一个接口和实现

public interface Formatter {

    String format(Object object);
}
public class DefaultFormatter implements Formatter{

    @Override
    public String format(Object object) {
        return String.valueOf(object);
    }
}

(4) 配置类

@Configuration
public class FormatterAutoConfiguration {

    @Bean
    public Formatter defaultFormatter(){
        return new DefaultFormatter();
    }
}

(5) 配置文件
写一个META-INF/spring.factories文件,内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=thinking.in.spring.boot.config.FormatterAutoConfiguration

完成之后如果需要使用就引入这个项目,就可以直接使用里面的接口了.