2011-11-28 11 views

risposta

4

Utilizzare una consuetudine PropertyPlaceholderConfigurer implementation che sostituisce i metodi resolve... e registra il nome placeholder. Potrebbe anche essere necessario/voler sovrascrivere i metodi convert..., ma lo resolve... dovrebbe gestirlo.

5

Questa è la mia realizzazione:

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

la classe base non ha il metodo –

+0

Debolezza della soluzione - se qualsiasi proprietà ha un errore - non vedrai mai tutto questo proprietà nel file di registro poiché afterPropertiesSet() non verrà richiamato. – rauch

+4

Non funziona per me, nessuna proprietà nella lista. –

1

Ecco un esempio concreto di stampa di tutte le proprietà:

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 debolezza di questa soluzione è che stampa tutte le proprietà di mappatura delle fonti singolarmente, possibilmente, valori duplicati per le chiavi sovrascritte invece di stampare solo i valori dei risultati per tutte le proprietà. –

Problemi correlati