【spring-boot】自定義starter

一. 命名規(guī)范

springboot提供的starter都是以spring-boot-starter-xxx的方式命名的,針對自定義的starter,官方建議以xxx-spring-boot-starter命名予以區(qū)分。見官方說明

二. 依賴

    <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>

可以將其他的必要的依賴加入進(jìn)來。

三. 寫法

1. META-INF

在resources下新建包META-INF,并新增文件spring.factories。內(nèi)容如下:

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);
          }
      
      }
      

解釋:當(dāng)項目依賴該starter,并且配置文件中包含hello.service為前綴且hello.service.enable為true時,就會自動生成HelloService的bean。

附上代碼庫 hello-spring-boot-starter

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容