package com.hx.auto.manage.xml.scan.util; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.PropertyResourceConfigurer; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.core.io.support.PropertiesLoaderSupport; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.Properties; @Component public class ConfigUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; private static Properties properties = null; @Value(Constants.CJH_SCAN_XML_SUPPLY) private String isScan; @Value(Constants.CJH_PACKAGE_FILE_NAME_URL) private String packFileName; @Value(Constants.CJH_PACKAGE_MODEL_URL) private String modelPack; @Value(Constants.CJH_CREATE_XML_URL) private String xmlUrl; @Value(Constants.CJH_CREATE_DAO_URL) private String daoUrl; /** * 实现ApplicationContextAware接口的回调方法,设置上下文环境 */ @Override public void setApplicationContext(ApplicationContext applicationContext) { ConfigUtil.applicationContext = applicationContext; } /** * 获得spring上下文 * * @return ApplicationContext spring上下文 */ public ApplicationContext getApplicationContext() { return applicationContext; } /** * 获取bean * * @param name * service注解方式name为小驼峰格式 * @return Object bean的实例对象 */ public Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } 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); } } public static Properties getProperties() { return properties; } public static void setProperties(Properties properties) { ConfigUtil.properties = properties; } public String getModelPack() { return modelPack; } public void setModelPack(String modelPack) { this.modelPack = modelPack; } public Boolean getIsScan() { return "true".equals(isScan); } public void setIsScan(String isScan) { this.isScan = isScan; } public String getPackFileName() { return packFileName; } public void setPackFileName(String packFileName) { this.packFileName = packFileName; } public String getXmlUrl() { return xmlUrl; } public void setXmlUrl(String xmlUrl) { this.xmlUrl = xmlUrl; } public String getDaoUrl() { return daoUrl; } public void setDaoUrl(String daoUrl) { this.daoUrl = daoUrl; } }