Desidero stampare l'elenco consolidato delle proprietà impostato nella nostra applicazione all'avvio. Qual è il modo migliore per farlo?Stampa di tutte le proprietà impostate tramite Spring PropertyPlaceholderConfigurer
Grazie
Desidero stampare l'elenco consolidato delle proprietà impostato nella nostra applicazione all'avvio. Qual è il modo migliore per farlo?Stampa di tutte le proprietà impostate tramite Spring PropertyPlaceholderConfigurer
Grazie
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.
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();
}
}
}
la classe base non ha il metodo –
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
Non funziona per me, nessuna proprietà nella lista. –
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);
}
}
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à. –
Possibile duplicato di [Primavera: accesso tutte le proprietà ambiente come un oggetto Map o Proprietà] (https://stackoverflow.com/questions/23506471/spring-access-all-environment-properties-as-a-map-or-properties-object) – Cherry