1. 程式人生 > 其它 >Spring Boot自定義Starter編寫Demo

Spring Boot自定義Starter編寫Demo

Spring Boot的自定義Starter

​ 一般建立自定義Starter時,會分為兩個專案:xxx-spring-boot-starter, xxx-spring-boot-autoconfigure。starter模組不寫任何業務程式碼,只用來引入autoconfigure模組的依賴,而在其他專案引入該自定義starter時,直接引用starter模組的依賴即可。

​ 下面是編寫自定義starter的流程:

  1. 建立兩個模組:

    這裡以example-spring-boot-starterexample-spring-boot-autoconfigure為例,直接建立Maven專案即可。

  2. 編寫starter模組的配置

    starter模組的pom檔案:

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    >
    <modelVersion>4.0.0</modelVersion> <artifactId>example-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <groupId>org.example</groupId> <packaging>jar</packaging> <name>example-spring-boot-starter</
    name
    >
    <dependencies> <!--starter模組只需要匯入autoconfigure模組的依賴即可--> <dependency> <groupId>org.example</groupId> <artifactId>example-spring-boot-autoconfigure</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
  3. 編寫autoconfigure模組的配置

    autoconfigure模組的pom檔案:

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <groupId>org.example</groupId>
        <artifactId>example-spring-boot-autoconfigure</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <parent>
            <artifactId>spring-boot-starter-parent</artifactId>
            <groupId>org.springframework.boot</groupId>
            <version>2.1.12.RELEASE</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        
        <dependencies>
            <!-- springboot starter依賴 -->
            <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>
        </dependencies>
    </project>
    
  4. 在autoconfigure模組編寫對應的具體程式碼:

    1. ExampleAutoConfigure(配置類)

      @Configuration	// 標註配置類
      @EnableConfigurationProperties(ExampleProperties.class)	// 在有配置時生效
      @ConditionalOnProperty(	// 在配置了org.example.enabled屬性為true時,配置類才會生效
              prefix = "org.example",
              name = "enabled",
              havingValue = "true"
      )
      public class ExampleAutoConfigure {
          @Autowired
          private ExampleProperties exampleProperties;
      
          // 當容器中沒有ExampleService時,注入
          @ConditionalOnMissingBean(ExampleService.class)
          @Bean
          public ExampleService exampleService() {
              return new ExampleService(exampleProperties);
          }
      }
      
    2. ExampleProperties(配置檔案對應)

      @ConfigurationProperties(prefix = "org.example")	// 設定配置檔案的字首
      public class ExampleProperties {
          private String name;
          private String content;
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public String getContent() {
              return content;
          }
      
          public void setContent(String content) {
              this.content = content;
          }
      }
      
      
    3. ExampleService(業務類)

      public class ExampleService {
          private ExampleProperties exampleProperties;
      
          public ExampleService() {
      
          }
      
          public ExampleService(ExampleProperties exampleProperties) {
              this.exampleProperties = exampleProperties;
          }
      
          public String testExample() {
              return "ExampleService : " + exampleProperties.getName() + " -- " + exampleProperties.getContent();
          }
      }
      
  5. 編寫spring.factories檔案:

    注意,這個檔案必須在autoconfigure模組的resources/META-INF/spring.factories

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.example.config.ExampleAutoConfigure
    
  6. 在其他專案引入進行測試

    在其他專案中引入時,只需要直接引入example-spring-boot-starter依賴,在配置檔案進行對應的配置後,就可以使用了。

    pom檔案:

    <dependency>
        <groupId>org.example</groupId>
        <artifactId>example-spring-boot-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    

    測試介面:

    @RestController
    public class TestController {
        @Autowired
        private ExampleService exampleService;
    
        @GetMapping("/print")
        public String print() {
            System.out.println("..........");
    
            return exampleService.testExample();
        }
    }
    

    配置檔案application.yml

    server:
      port: 7888
    org:
      example:
        enabled: true
        name: sprog
        content: 你好世界
    
  7. 訪問介面的結果:

在這裡插入圖片描述
自定義Starter已經可以使用了。