应用程序在使用XML方式集成Spring的时候,会使用<context:property-placeholder location="conf.properties" />配置让Spring对properties文件进行管理。有时候需要动态的获取properties文件参数配置信息,可以参照以下方式:
PropertySourcesPlaceholderConfigurer bean = context.getBean(PropertySourcesPlaceholderConfigurer.class);
PropertySources propertySources = bean.getAppliedPropertySources();
PropertySource<?> propertySource = propertySources.get(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME);
String value= propertySource.getProperty(属性Key).toString();
注意导入spring的静态常量:
import static org.springframework.context.support.PropertySourcesPlaceholderConfigurer.LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME;