08_springboot2.xカスタムスターター

概要
スターター:スターター
1は、このシナリオでは、頼りにしてあるものを使用する必要が?
自動コンフィギュレーションを作成する方法を2、

ルール:

@Configurationは//このクラスを指定するクラス設定され
、自動的に指定された条件で有効であるように構成@ConditionalOnXXX //クラスが満たされ
@AutoConfigureAfter //クラス指定された順序自動設定
コンテナにコンポーネントを追加// @Bean
関連xxxPropertiesと@ConfigurationPropertie関連する構成バインドするクラス
@EnableConfigurationPropertiesを//はxxxPropertiesがコンテナに追加効果取らせ
、自動的にクラスをロードすることができるように構成が
自動的に起動する必要があります。META-INF / spring.factoriesを構成し、コンフィギュレーション・クラスをロードします

= org.springframework.boot.autoconfigure.EnableAutoConfiguration \
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration、\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration、\
パターン:
イネーブラーのみ依存性を導入するために使用される。
具体的に書き込みモジュールが自動的に設定され、
イニシエータは自動設定に依存している。他は唯一のランチャー(スターター)を導入する必要が
MyBatisの-春ブーツ・スターター;名前-springブート・スターターイニシエータカスタム

イネーブラ(スターター)
-スターターモジュールJARファイルは、自動アセンブリまたは他のライブラリを頼ることができる依存性管理の助けにあって、空であります

命名規則は:
•以下の命名規則を推奨しています

公式の名前空間
-プレフィックス:「春ブートstarter-」
-モード:春-ブートstarter-モジュール名
-例:春・ブート・スターター・ウェブ 、春・ブート・スターター・アクチュエータ、春・ブートstarter- JDBC

カスタム名前空間
-サフィックス:「 -春ブーツ・スターター」
-モード:ブート・スターター・モジュールは-spring
-例:MyBatisのスプリング・ブートスターター

ダイヤグラム

図1に示すように、スタータXXX-スターターの調製

図2に示すように、自動構成モジュール

pom.xml

<?xmlのバージョン= "1.0"エンコード= "UTF-8">
<プロジェクトのxmlns = "http://maven.apache.org/POM/4.0.0"のxmlns:XSI = "のhttp://www.w3 .ORG / 2001 / XMLスキーマ・インスタンス」
         のxsi:のschemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion> 4.0.0 </ modelVersion>
    <親>
        <のgroupId> org.springframework.boot </のgroupId>
        <たartifactId>ばねブートスタータ親</たartifactId>
        <バージョン> 2.1.9.RELEASE </バージョン>
        <relativePath /> <! -リポジトリから検索親- >
    </親>
    <groupIdを> com.spboot </ groupIdを>
    <たartifactId> jatpeo-スプリングブートスタータautoconfigurer </たartifactId>
    <バージョン> 0.0.1-SNAPSHOT </バージョン>
    <名前> jatpeoスプリング・ブート・スタータautoconfigurer </名前>
    <説明>春ブーツ用デモプロジェクト</記述>
 
    <プロパティ>
        <のjava.version> 1.8 </java.version>
    </プロパティ>
 
    <依存性>
        <! -所有スターター的基本配置- >
        <依存>
            <groupIdを> org.springframework.boot </ groupIdを>
            <たartifactId>春・ブート・スターター</たartifactId>
        </依存関係>
    </依存関係>
 
 
</プロジェクト>
应用举例:

HelloProperties配置文件
パッケージcom.spboot.starter。
 
輸入org.springframework.boot.context.properties.ConfigurationProperties。
 
@ConfigurationProperties(接頭辞=「jatpeo.hello」)
パブリッククラスHelloProperties {
 
    プライベート文字列の接頭辞。
    プライベート文字列の接尾辞。
 
    パブリック文字列のgetPrefix(){
        リターンプレフィックス。
    }
 
    公共ボイドsetPrefix(文字列の接頭辞){
        this.prefix =接頭辞。
    }
 
    パブリック文字列getSuffix(){
        リターンサフィックス。
    }
 
    公共ボイドsetSuffix(文字列のサフィックス){
        this.suffix =サフィックス。
    }
}
 

HelloServiceの
パッケージcom.spboot.starter。
 
パブリッククラスHelloService {
 
    HelloProperties helloProperties。
 
 
    パブリック文字列のsayHello(文字列名){
 
    リターンhelloProperties.getPrefix()+名+ helloProperties.getSuffix()。
    }
 
    パブリックHelloPropertiesのgetHelloProperties(){
        戻りhelloProperties。
    }
 
    公共ボイドsetHelloProperties(HelloProperties helloProperties){
        this.helloProperties = helloProperties。
    }
}
自动配置类。

HelloServiceAutoConfigurationの
パッケージcom.spboot.starter。
 
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.boot.autoconfigure.EnableAutoConfiguration。
輸入org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication。
輸入org.springframework.boot.context.properties.EnableConfigurationProperties。
輸入org.springframework.context.annotation.Bean;
輸入org.springframework.context.annotation.Configuration。
 
@Configuration
@ ConditionalOnWebApplication //ウェブ应用起效果
@EnableConfigurationProperties(HelloProperties.class)
パブリッククラスHelloServiceAutoConfiguration {
 
    @Autowired
    HelloProperties helloProperties。
 
    @Bean
    公共HelloService helloService(){
        HelloService helloService HelloService新しい新=();
        helloService.setHelloProperties(helloProperties);
        helloServiceを返す;
    }
}
注:これは、自動コンフィギュレーション・クラスを有効にでき得るMETA-INF /新spring.factories

= org.springframework.boot.autoconfigure.EnableAutoConfiguration \
com.spboot.starter.HelloServiceAutoConfiguration
のMavenリポジトリをインストールされています

インストール手順に注意してください。

テスト:
新規プロジェクト

pom.xml

注:リファレンス我々はスターターを設定しています

application.properties

コントローラ:

パッケージcom.spboot.springboot.controller。
 
輸入com.spboot.starter.HelloService。
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.web.bind.annotation.GetMapping。
輸入org.springframework.web.bind.annotation.RestController;
 
@RestController
パブリッククラスhelloController {
 
    @Autowired
    HelloService helloService。
 
    ( "ハロー/")@GetMapping
    パブリック文字列言う(){
        helloService.sayHello( "DYN")を返します。
    }
 
}
浏览器测试。

 

 

 

親指
お気に入り
を共有

jatpen
----------------
免責事項:この記事は元の記事CSDNブロガー「jatpen」で、CC 4.0 BY-SAの著作権契約書に従って、再現し、元のソースと、このリンクを添付してください声明。
オリジナルリンクします。https://blog.csdn.net/jatpen/article/details/102323726

公開された51元の記事 ウォン称賛80 ビュー930 000 +

おすすめ

転載: blog.csdn.net/xiyang_1990/article/details/103538816