下面列出了org.springframework.core.io.support.PropertiesLoaderSupport#org.springframework.beans.factory.config.PropertyResourceConfigurer 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private List<PropertySourcesPlaceholderConfigurer> getAllPropertySourcesPlaceholderConfigurers() {
if (!(this.beanFactory instanceof ListableBeanFactory)) {
return emptyList();
}
ListableBeanFactory listableBeanFactory = (ListableBeanFactory) this.beanFactory;
// take care not to cause early instantiation of flattenedPropertySources FactoryBeans
Map<String, PropertySourcesPlaceholderConfigurer> beans = listableBeanFactory
.getBeansOfType(PropertySourcesPlaceholderConfigurer.class, false, false);
List<PropertySourcesPlaceholderConfigurer> configurers = new ArrayList<>(beans.values());
configurers.sort(comparingInt(PropertyResourceConfigurer::getOrder));
return configurers;
}
private void initProperties() {
properties = new Properties();
try {
String[] postProcessorNames = applicationContext.getBeanNamesForType(BeanFactoryPostProcessor.class, true,
true);
for (String ppName : postProcessorNames) {
BeanFactoryPostProcessor beanProcessor = applicationContext.getBean(ppName,
BeanFactoryPostProcessor.class);
if (beanProcessor instanceof PropertyResourceConfigurer) {
PropertyResourceConfigurer propertyResourceConfigurer = (PropertyResourceConfigurer) beanProcessor;
Method mergeProperties = PropertiesLoaderSupport.class.getDeclaredMethod("mergeProperties");
mergeProperties.setAccessible(true);
Properties props = (Properties) mergeProperties.invoke(propertyResourceConfigurer);
// get the method convertProperties
// in class PropertyResourceConfigurer
Method convertProperties = PropertyResourceConfigurer.class.getDeclaredMethod("convertProperties",
Properties.class);
// convert properties
convertProperties.setAccessible(true);
convertProperties.invoke(propertyResourceConfigurer, props);
properties.putAll(props);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Bean
public static PropertyResourceConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}