Spring Boot 1.4.x の Web アプリを 1.5.x へバージョンアップする ( その10 )( 起動時の spring.profiles.active のチェック処理を Set.contains を使用した方法に変更する )
概要
Spring Boot 1.4.x の Web アプリを 1.5.x へバージョンアップする ( その9 )( Spring Boot を 1.5.3 → 1.5.4 にバージョンアップする ) の続きです。
- 今回の手順で確認できるのは以下の内容です。
- IntelliJ IDEA 2017.2 Public Preview の記事が出ていたので読んでみたのですが、その中の「Replacing multiple equals with Set.contains」を見て、Application クラスで spring.profiles.active をチェックしている処理に適用できそうなので、変更することにします。
参照したサイト・書籍
- IntelliJ IDEA 2017.2 Public Preview
https://blog.jetbrains.com/idea/2017/06/intellij-idea-2017-2-public-preview/
目次
手順
Application クラスの spring.profiles.active のチェック処理を Set.contains を使用した方法に変更する
src/main/java/ksbysample/webapp/lending/Application.java を以下のように変更します。
public class Application { private static final Set<String> springProfiles = Collections .unmodifiableSet(new HashSet<>(Arrays.asList("product", "develop", "unittest"))); /** * Spring Boot メインメソッド * * @param args ??? */ public static void main(String[] args) { String springProfilesActive = System.getProperty("spring.profiles.active"); if (!springProfiles.contains(springProfilesActive)) { throw new UnsupportedOperationException( MessageFormat.format("JVMの起動時引数 -Dspring.profiles.active で " + "develop か unittest か product を指定して下さい ( -Dspring.profiles.active={0} )。" , springProfilesActive)); } SpringApplication.run(Application.class, args); } }
private static final Set<String> springProfiles = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("product", "develop", "unittest")));
を追加します。- main メソッド内の if 文で springProfilesActive を StringUtils.equals でチェックしていた処理を
!springProfiles.contains(springProfilesActive)
に変更します。
動作を確認してみます。bootRun タスクで Tomcat を起動してみると、正常に起動します。
Tomcat を停止した後、build.gradle の bootRun に記述している spring.profiles.active を develop
→ developx
に変更してから再度 bootRun で Tomcat を起動しようとしてみると、
bootRun {
jvmArgs = ['-Dspring.profiles.active=developx']
}
今度はエラーになって起動しませんでした。チェックは正常に機能しているようです。
clean タスク → Rebuild Project → build タスクを実行すると “BUILD SUCCESSFUL” のメッセージが出力されることも確認できます。
IntelliJ IDEA 2017.2 は「Spring Boot: dashboard & actuator endpoints」が一番期待している機能なのですが、他にもいろいろ面白そうだったり、よく分からない機能があって、リリースが楽しみです。でも GIFアニメーションがちょっと速すぎて、もう少しゆっくり見たいんだけどな、とも思いました。。。
履歴
2017/06/23
初版発行。