복붙노트

[SPRING] 콩 인스턴스화 전에 스프링 부팅 응용 프로그램의 모든 활성 속성을 기록하는 방법?

SPRING

콩 인스턴스화 전에 스프링 부팅 응용 프로그램의 모든 활성 속성을 기록하는 방법?

현재 활성 구성 로깅을 묻는 질문이 있지만 올바른 대답이 있지만 문제는 모든 Bean이 올바르게 인스턴스화 된 경우에만 구성이 로깅된다는 것입니다. 시작할 때 응용 프로그램이 충돌하는 경우 (주로) 모든 속성을 기록하고 싶습니다. 제 질문은 좀 더 구체적입니다.

콩 인스턴스화 전에 스프링 부팅 응용 프로그램의 모든 활성 속성을 기록하는 방법?

해결법

  1. ==============================

    1.이렇게하려면 ApplicationListener를 등록해야합니다. 잡을 이벤트는 문서에 따라 ApplicationPreparedEvent입니다.

    이렇게하려면 ApplicationListener를 등록해야합니다. 잡을 이벤트는 문서에 따라 ApplicationPreparedEvent입니다.

    주요 방법은 다음과 같습니다.

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

    현재 질문에서 인용 된 답변의 코드를 재사용했지만 얻은 컨텍스트가 아직 새로 고쳐지지 않았고 환경 구조가 응용 프로그램 시작 후와 정확히 같지 않기 때문에 수정했습니다. 또한 속성 소스별로 속성을 인쇄했습니다. 하나는 시스템 환경 용이고 다른 하나는 시스템 속성 용이고 다른 하나는 응용 프로그램 구성 속성 용입니다.

    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;
    
      @Override
      public void onApplicationEvent(ApplicationPreparedEvent event) {
        environment = event.getApplicationContext().getEnvironment();
        printProperties();
      }
    
      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;
      }
    }
    
  2. from https://stackoverflow.com/questions/48212761/how-to-log-all-active-properties-of-a-spring-boot-application-before-the-beans-i by cc-by-sa and MIT license