编码方式获取Spring中PropertyPlaceholderConfigurer的属性

时间:2025-02-13 18:59:40
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Created by 刘一波 on 15/8/5. * E-Mail:@ */ @Component public class SpringPropertyResourceReader implements ApplicationContextAware { private static ApplicationContext applicationContext; private static AbstractApplicationContext abstractContext; private static Properties properties = new Properties(); private static void init() { try { // get the names of BeanFactoryPostProcessor String[] postProcessorNames = (, true, true); for (String ppName : postProcessorNames) { // get the specified BeanFactoryPostProcessor BeanFactoryPostProcessor beanProcessor = (ppName, ); // check whether the beanFactoryPostProcessor is // instance of the PropertyResourceConfigurer // if it is yes then do the process otherwise continue if (beanProcessor instanceof PropertyResourceConfigurer) { PropertyResourceConfigurer propertyResourceConfigurer = (PropertyResourceConfigurer) beanProcessor; // get the method mergeProperties // in class PropertiesLoaderSupport Method mergeProperties = . getDeclaredMethod("mergeProperties"); // get the props (true); Properties props = (Properties) mergeProperties. invoke(propertyResourceConfigurer); // get the method convertProperties // in class PropertyResourceConfigurer Method convertProperties = . getDeclaredMethod("convertProperties", ); // convert properties (true); (propertyResourceConfigurer, props); (props); } } } catch (Exception e) { throw new RuntimeException(e); } } public static String getProperty(String propertyName) { return (propertyName); } @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { applicationContext = arg0; abstractContext = (AbstractApplicationContext) applicationContext; init(); } }