2011-11-28 2 views
7

起動時にアプリケーションで設定されたプロパティの統合リストを印刷したいと思います。これを行う最善の方法は何ですか?Spring PropertyPlaceholderConfigurerによって設定されたすべてのプロパティを印刷する

おかげ

+1

の可能性のある重複した[春:地図やPropertiesオブジェクトとしてアクセスすべての環境のプロパティ]( https://stackoverflow.com/questions/23506471/spring-access-all-environment-properties-as-a-map-or-properties-object) – Cherry

答えて

4

resolve...メソッドをオーバーライドし、placeholder名をログに記録するカスタムPropertyPlaceholderConfigurer implementationを使用してください。また、convert...メソッドをオーバーライドする必要があるかもしれませんが、resolve...はそれを処理する必要があります。

5

これは私の実装である:ここでは

public class CustomPropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer implements InitializingBean{ 

    public void afterPropertiesSet(){ 
     try{ 
      Properties loadedProperties = this.mergeProperties(); 
      for(Entry<Object, Object> singleProperty : loadedProperties.entrySet()){ 
       logger.info("LoadedProperty: "+singleProperty.getKey()+"="+singleProperty.getValue()); 
      } 
     }catch(Exception ex){ 
      ex.printStackTrace(); 
     } 
    } 
} 
+0

基本クラスにはメソッドがありません –

+0

ソリューションの脆弱性 - ifどんなプロパティにもエラーがあります。 afterPropertiesSet()は呼び出されないので、ログファイルのプロパティーは呼び出されません。 – rauch

+4

私にとっては機能しません。リストには全く属性がありません。 –

1

は、すべてのプロパティを印刷する具体的な例である:

import org.springframework.beans.BeansException; 
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 
import org.springframework.web.context.support.StandardServletEnvironment; 

public class PropertiesLoaderConfigurer 
    extends PropertySourcesPlaceholderConfigurer { 

    private static final String ENVIRONMENT_PROPERTIES = "environmentProperties"; 

    @Override 
    public void postProcessBeanFactory(
     final ConfigurableListableBeanFactory beanFactory) 
     throws BeansException { 

     super.postProcessBeanFactory(beanFactory); 

     final StandardServletEnvironment propertySources = 
      (StandardServletEnvironment) super.getAppliedPropertySources().get(ENVIRONMENT_PROPERTIES).getSource(); 

     propertySources.getPropertySources().forEach(propertySource -> { 
      if (propertySource.getSource() instanceof Map) { 
       // it will print systemProperties, systemEnvironment, application.properties and other overrides of 
       // application.properties 
       System.out.println("#######" + propertySource.getName() + "#######"); 

       final Map<String, String> properties = mapValueAsString((Map<String, Object>) propertySource.getSource()); 
       System.out.println(properties); 
      } 
     }); 
    } 

    private Map<String, String> mapValueAsString(
     final Map<String, Object> map) { 

     return map.entrySet().stream() 
      .collect(Collectors.toMap(entry -> entry.getKey(), entry -> toString(entry.getValue()))); 
    } 

    private String toString(
     final Object object) { 

     return Optional.ofNullable(object).map(value -> value.toString()).orElse(null); 
    } 
} 
+0

このソリューションの弱点は、すべてのプロパティの結果値のみを出力するのではなく、オーバーライドされたキーの複製可能な値を個別にすべてのソースプロパティマッピングに出力することです。 –