カスタムSpring-Boot-Starterを開発


Spring Bootは多くのスターで構成されています.バージョンの推移に伴い、Starter家族のメンバーも日増しに増えています.従来のMavenプロジェクトでは、しばしばいくつかの層、コンポーネントをモジュールに分割して管理し、相互に多重化に依存するため、Spring Bootプロジェクトでは、カスタムSpring Boot Starterを作成して目的を達成することができる.
一:まずMavenプロジェクトを作成して、依存のpom.xmlファイルを導入します.


    4.0.0
    com.example
    example-spring-boot-starter
    1.0-SNAPSHOT
    
        
            org.springframework.boot
            spring-boot-autoconfigure
        
    
    
        
            
                
                org.springframework.boot
                spring-boot-dependencies
                1.5.2.RELEASE
                pom
                import
            
        
    

ここではartiftIdの命名問題について、Spring公式Starterは一般的にspring-boot-starter-nameと命名されています.spring-book-starter-webのように、Spring公式Starterの名前は「name」-spring-boot-starterのフォーマットに従うべきです.
ここで私達のStarterが実現する機能は簡単で、Serviceを提供します.文字列を前の接尾辞に加えることができる方法String wrapを含みます.
public class ExampleService {

    private String prefix;
    private String suffix;

    public ExampleService(String prefix, String suffix) {
        this.prefix = prefix;
        this.suffix = suffix;
    }
    public String wrap(String word) {
        return prefix + word + suffix;
    }
}
プレフィックス、サフィックスは、appication.properties(yml)内のパラメータを読むことによって得られます.
@ConfigurationProperties("example.service")
public class ExampleServiceProperties {
    private String prefix;
    private String suffix;
    //setter and getter   
}
重点としてAutoConfigure類を編纂する.
@Configuration
@ConditionalOnClass(ExampleService.class)
@EnableConfigurationProperties(ExampleServiceProperties.class)
public class ExampleAutoConfigure {

    @Autowired
    private ExampleServiceProperties properties;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true")
    ExampleService exampleService (){
        return  new ExampleService(properties.getPrefix(),properties.getSuffix());
    }
}
説明で使用するいくつかのスターターに関するコメント:
@Contintional OnClassは、クラスパスの下でこのクラスを発見した場合、自動配置を行う.@Conditional OnMissingBenは、Spring Contectの中にこのBeanが存在しない場合@Coditional OnProperty(prefix=「example.service」、value=「enabled」、havingValue=「true」)は、プロファイルの中でexample.service.enabed=trueとなる場合.詳細については、公式文書を読むことをお勧めします.
最後のステップは、レスポンス/META-INF/でspring.factoresファイルを作成します.
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.autocinfigure.ExampleAutoConfigure
OK、完了、運行mvn:installパッケージインストール、Spring Boot Starterが開発されました.
二:springbootプロジェクトを作成し、先ほどカスタムのstarterを参照してください.
example-spring-boot-starter依存を導入します.

    com.example
    example-spring-boot-starter
    1.0-SNAPSHOT
 
appicationを作成します.yml
example.service:
  enabled: true
  prefix: ppp
  suffix: sss
簡単なSpring Webアプリを作成し、StarterからのExampleServiceを注入して正常に動作するかどうかを確認します.
import com.example.autocinfigure.ExampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@SpringBootApplication
@RestController
public class BootApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class, args);
    }

    @Resource
    private ExampleService exampleService;

    @GetMapping("/input")
    public String input() {
        return exampleService.wrap("wpz");
    }
}
アクセスhttp://localhost:8080/input
スターの仕事原理をまとめます.
Spring Bootは起動時にスキャン項目に依存するJARバッグを探しています.spring.factoresファイルを含むJARカバンを探しています.spring.factoresの配置によってAutoConfigure類をロードします.@Conditionalの注釈の条件に基づいて、自動配置して、BenをSpring ContingControtextに注入します.