2011-11-28 6 views
7

Me gustaría imprimir la lista consolidada de propiedades configuradas en nuestra aplicación al inicio. ¿Cuál es la mejor manera de hacer esto?Imprimir todas las propiedades configuradas mediante Spring PropertyPlaceholderConfigurer

Gracias

+1

Posible duplicado de [Primavera: acceso a todas las propiedades de entorno como un objeto de mapa o Propiedades] (https://stackoverflow.com/questions/23506471/spring-access-all-environment-properties-as-a-map-or-properties-object) – Cherry

Respuesta

4

Utilice una costumbre PropertyPlaceholderConfigurer implementation que anula los métodos resolve... y registra el nombre placeholder. También puede necesitar/querer anular los métodos convert..., pero resolve... debe manejarlo.

5

Esta es mi aplicación:

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

clase base no tiene el método –

+0

Debilidad de su solución - si cualquier propiedad tiene un error: nunca verás todo esto propiedades en el archivo de registro ya que afterPropertiesSet() no se invocará. – rauch

+4

No funciona para mí, no hay propiedades en la lista. –

1

Aquí es un ejemplo concreto de impresión de todas las propiedades:

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

La debilidad de esta solución es que imprime todos los mapeos de propiedades de orígenes, posiblemente valores duplicados para claves anuladas en lugar de imprimir solo los valores de resultado para todas las propiedades. –

Cuestiones relacionadas