การพิมพ์คุณสมบัติทั้งหมดที่ตั้งค่าผ่าน Spring PropertyPlaceholderConfigurer

ฉันต้องการพิมพ์รายการคุณสมบัติรวมที่ตั้งไว้ในแอปพลิเคชันของเราเมื่อเริ่มต้นระบบ วิธีที่ดีที่สุดในการทำเช่นนี้คืออะไร?

ขอบคุณ


person Darth Ninja    schedule 28.11.2011    source แหล่งที่มา


คำตอบ (3)


ใช้ ที่กำหนดเอง PropertyPlaceholderConfigurer การใช้งานที่แทนที่วิธีการ resolve... และบันทึกชื่อ placeholder คุณอาจต้องการ/ต้องการแทนที่วิธีการ convert... แต่ resolve... ควรจัดการมัน

person Dave Newton    schedule 28.11.2011

นี่คือการดำเนินการของฉัน:

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();
        }
    }
}
person fl4l    schedule 19.01.2014
comment
คลาสพื้นฐานไม่มีวิธีการ - person Daneel Yaitskov; 19.10.2016
comment
จุดอ่อนของโซลูชันของคุณ - หากคุณสมบัติใดมีข้อผิดพลาด - คุณจะไม่เห็นคุณสมบัติเหล่านี้ทั้งหมดในไฟล์บันทึกเนื่องจาก afterPropertiesSet() จะไม่ถูกเรียกใช้ - person rauch; 12.12.2016
comment
ใช้งานไม่ได้สำหรับฉัน ไม่มีคุณสมบัติในรายการเลย - person Daniel Hári; 01.03.2017

นี่คือตัวอย่างที่ชัดเจนของการพิมพ์คุณสมบัติทั้งหมด:

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);
    }
}
person amgohan    schedule 15.04.2016
comment
จุดอ่อนของโซลูชันนี้คือ พิมพ์การแมปคุณสมบัติแหล่งที่มาทั้งหมดทีละค่าที่อาจซ้ำกันสำหรับคีย์ที่ถูกแทนที่ แทนที่จะพิมพ์เฉพาะค่าผลลัพธ์สำหรับคุณสมบัติทั้งหมด - person Daniel Hári; 01.03.2017