Spring Bootはスターターを使用して多くの構成の問題を解決しますが、これらの問題をどのように解決しましたか?
ここでは、簡単な例を使用して、スターターがデフォルトの構成を設定する方法を確認します。
1.スタータープロジェクトを作成します
カスタムスターター、プロジェクトの命名仕様は次のとおりです
私の最後のディレクトリ構造を見てみましょう
1. pom.xmlファイルを変更します
<Project XMLNS = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://maven.apach/4.0. http://maven.apache.org/xsd/maven-4.0.0.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> org.elvin </groupid> <artifactid> my-spring-boot-starter </artifactid> 1.0-snapshot </packaging> <name> my-spring-boot-starter </name> <url> http://maven.apache.org </url> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> </properties> <artifactid> spring-boot-autoconfigure </artifactid> <version> 1.5.9.Release </version> </dependency> <dependency> <groupid> junit </groupid> <artifactid> junit </artifactid> <バージョン> 3.8.1 </バージョン> <scope>テスト<groupid> org.apache.maven.plugins </groupid> <artifactid> maven-compiler-plugin </artifactid> <バージョン> 2.3.2 </version> <configuration> <source> 1.8 </source> 1.8 </ターゲット> </configuration> </plugin> </blubins> </bultins> </project> </project>
実際、Spring-Boot-autoconfigureを追加しました
アプリファイルのメインメソッドにコメントしましたが、これはここでは使用されていません
2。属性に対応する受信ファイルを構成します
Package org.elvin; Import org.springframework.boot.context.properties.configurationProperties;/** *著者:elvin *日付:2017/12/12 14:51 *説明: */@configurationproperties(prefix = "hello")公開クラスのhelloserviceproperties {// private stringプライベート文字列msg = msg; public string getMsg(){return msg; } public void setmsg(string msg){this.msg = msg; }}3。外部サービス
パッケージorg.elvin;/** *著者:elvin *日付:2017/12/12 14:55 *説明: */public class helloservice {private string msg; public string sayshello(){return "hello" + msg; } public string getMsg(){return msg; } public void setmsg(string msg){this.msg = msg; }}4.外部サービスは、対応するファイルに関連付けられています
Package org.elvin; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.autoconfigure.conditionalonclass; Import org.springframework.boot.autoconfigure.condition.conditionalonmissinginginginginging org.springframework.boot.autoconfigure.condition.conditionalonproperty; Import org.springframework.boot.context.properties.enableconfigurationproperties; Import org.springframework.context.annotation.bean; Import org.springframework.annotation.configuration;/** *著者:elvin *日付:2017/12/12 14:59 *説明: */@configuration@enableconfigurationProperties(helloserviceproperties.class)@conditionalonclass(helloservice.class(frent "" conditionalonclass)@conditionalonclass value = "enabled"、matchifmissing = true)public class helloserviceautoconfiguration {@autowired private helloserviceproperties helloserviceproperties; @bean @conditionalonmissingbean(helloservice.class)public helloservice helloservice(){helloservice helloservice = new helloservice(); helloservice.setmsg(helloserviceproperties.getmsg()); HelloServiceを返します。 }}5。スターター構成:Spring.Factories
org.springframework.boot.autoconfigure.enableautoconfiguration = org.elvin.helloserviceautoconfiguration
これを行った後、MVNクリーンインストールを使用してMavenライブラリにパッケージ化します
2。スプリングブートプロジェクトの使用
新しいスプリングブートプロジェクトを作成し、Webを選択します。
ディレクトリ構造:
参照pom.xmlを見てみましょう
<Dependency> groupId> org.elvin </groupid> <artifactid> my-spring-boot-starter </artifactid> <バージョン> 1.0-snapshot </version> </dependency>
Hellocontrollerをご覧ください
Package org.elvin.Learn.springBoot.Controller; Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; org.springframework.web.bind.annotation.restcontroller; Import org.elvin。 @RequestMapping( "index")public string index(){return helloservice.sayhello(); }}ここのHelloServiceは、以前のカスタムスターターにあります。
1。結果:構成されていない場合、それは出力HelloWorldにする必要があります
2。構成ファイルで、hello.msg = hahahahahahを追加します
この例は非常に簡単で、主なプロセスを示すためだけで、残りは各プラグインの論理的判断です。
参考文献:
Javaee開発の破壊者であるSpring Boot、練習バトル
Spring Bootカスタムスターターの実装に関する上記のチュートリアルは、私があなたと共有したすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。