【spring-boot】自定义starter

一. 命名规范

springboot提供的starter都是以spring-boot-starter-xxx的方式命名的,针对自定义的starter,官方建议以xxx-spring-boot-starter命名予以区分。见官方说明

二. 依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

可以将其他的必要的依赖加入进来。

三. 写法

1. META-INF

在resources下新建包META-INF,并新增文件spring.factories。内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=xxx.HelloStarterEnableAutoConfiguration

其中xxx为自动配置的核心类。写法是固定的,springboot会扫描该文件作为配置类。

2.核心注解

@Configuation

@EnableConfiguationProerties

@ConditionalOnProperties

  1. HelloStarterEnableAutoConfiguration

    @Configuration
    @ConditionalOnClass(HelloService.class)
    @EnableConfigurationProperties(HelloServiceProperties.class)
    public class HelloStarterEnableAutoConfiguration {
    
        private final HelloServiceProperties helloServiceProperties;
    
        @Autowired
        public HelloStarterEnableAutoConfiguration(HelloServiceProperties helloServiceProperties) {
            this.helloServiceProperties = helloServiceProperties;
        }
    
        @Bean
        @ConditionalOnProperty(prefix = "hello.service", name = "enable", havingValue = "true")
        HelloService helloService() {
            return new HelloService(helloServiceProperties.getPrefix(), helloServiceProperties.getSuffix());
        }
    
    }
    
  1. HelloServiceProperties

    @ConfigurationProperties("hello.service")
    public class HelloServiceProperties {
    
        private String prefix;
    
        private String suffix;
    
        public String getPrefix() {
            return prefix;
        }
    
        public void setPrefix(String prefix) {
            this.prefix = prefix;
        }
    
        public String getSuffix() {
            return suffix;
        }
    
        public void setSuffix(String suffix) {
            this.suffix = suffix;
        }
    }
    
    1. HelloService

      public class HelloService {
      
          private String prefix;
      
          private String suffix;
      
          public HelloService(String prefix, String suffix) {
              this.prefix = prefix;
              this.suffix = suffix;
          }
      
          public String say(String text) {
              return String.format("%s , hi , %s , %s", prefix, text, suffix);
          }
      
      }
      

解释:当项目依赖该starter,并且配置文件中包含hello.service为前缀且hello.service.enable为true时,就会自动生成HelloService的bean。

附上代码库 hello-spring-boot-starter

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容