Bean のインスタンス化前に Spring Boot アプリケーションのすべてのアクティブなプロパティをログに記録するにはどうすればよいですか? 質問する

Bean のインスタンス化前に Spring Boot アプリケーションのすべてのアクティブなプロパティをログに記録するにはどうすればよいですか? 質問する

すでに質問アクティブな構成のログ記録を要求すると、正しい答えしかし、問題は、すべての Bean が正しくインスタンス化された場合にのみ構成がログに記録されることです。起動時にアプリケーションがクラッシュした場合でも (主に) すべてのプロパティをログに記録したいと思います。私の質問はより具体的です:

Spring Boot アプリケーションのすべてのアクティブなプロパティをログに記録する方法前にBean のインスタンス化ですか?

ベストアンサー1

これを行うには、ApplicationListenerキャッチするイベントはApplicationPreparedEventドキュメントによると、

ApplicationPreparedEvent は、SpringApplication が起動し、ApplicationContext が完全に準備されているが、更新されていないときに発行されるイベントです。この段階で、Bean 定義がロードされ、環境が使用可能になります。

主なメソッドは次のようになります。

public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(MyApplication.class);
        springApplication.addListeners(new PropertiesLogger());
        springApplication.run(args);        
}

現在の質問で引用されている回答のコードを再利用しましたが、取得するコンテキストがまだ更新されておらず、環境の構造がアプリケーションの起動後とまったく同じではないため、コードを変更しました。また、プロパティ ソース別にプロパティを出力しました。1 つはシステム環境用、1 つはシステム プロパティ用、1 つはアプリケーション構成プロパティ用などです。また、はApplicationPreparedEvent複数回トリガーできること、およびプロパティが出力されるのは最初の 1 回のみであることにも注意してください。Spring Boot の問題 #8899詳細については。

package com.toto.myapp.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.PropertySource;

import java.util.LinkedList;
import java.util.List;

public class PropertiesLogger implements ApplicationListener<ApplicationPreparedEvent> {
  private static final Logger log = LoggerFactory.getLogger(PropertiesLogger.class);

  private ConfigurableEnvironment environment;
  private boolean isFirstRun = true;

  @Override
  public void onApplicationEvent(ApplicationPreparedEvent event) {
    if (isFirstRun) {
      environment = event.getApplicationContext().getEnvironment();
      printProperties();
    }
    isFirstRun = false;
  }

  public void printProperties() {
    for (EnumerablePropertySource propertySource : findPropertiesPropertySources()) {
      log.info("******* " + propertySource.getName() + " *******");
      String[] propertyNames = propertySource.getPropertyNames();
      Arrays.sort(propertyNames);
      for (String propertyName : propertyNames) {
        String resolvedProperty = environment.getProperty(propertyName);
        String sourceProperty = propertySource.getProperty(propertyName).toString();
        if(resolvedProperty.equals(sourceProperty)) {
          log.info("{}={}", propertyName, resolvedProperty);
        }else {
          log.info("{}={} OVERRIDDEN to {}", propertyName, sourceProperty, resolvedProperty);
        }
      }
    }
  }

  private List<EnumerablePropertySource> findPropertiesPropertySources() {
    List<EnumerablePropertySource> propertiesPropertySources = new LinkedList<>();
    for (PropertySource<?> propertySource : environment.getPropertySources()) {
      if (propertySource instanceof EnumerablePropertySource) {
        propertiesPropertySources.add((EnumerablePropertySource) propertySource);
      }
    }
    return propertiesPropertySources;
  }
}

おすすめ記事