chenjiahe
5 天以前 826b66207dafbce24f441cb83fed1b241a6fba27
提交 | 用户 | age
826b66 1 package com.hx.auto.manage.xml.scan.util;
C 2
3 import org.springframework.beans.BeansException;
4 import org.springframework.beans.factory.annotation.Value;
5 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
6 import org.springframework.beans.factory.config.PropertyResourceConfigurer;
7 import org.springframework.context.ApplicationContext;
8 import org.springframework.context.ApplicationContextAware;
9 import org.springframework.core.io.support.PropertiesLoaderSupport;
10 import org.springframework.stereotype.Component;
11
12 import java.lang.reflect.Method;
13 import java.util.Properties;
14
15 @Component
16 public class ConfigUtil implements ApplicationContextAware {
17
18     private static ApplicationContext applicationContext;
19
20     private static Properties properties = null;
21
22     @Value(Constants.CJH_SCAN_XML_SUPPLY)
23     private String isScan;
24
25     @Value(Constants.CJH_PACKAGE_FILE_NAME_URL)
26     private String packFileName;
27
28     @Value(Constants.CJH_PACKAGE_MODEL_URL)
29     private String modelPack;
30
31     @Value(Constants.CJH_CREATE_XML_URL)
32     private String xmlUrl;
33
34     @Value(Constants.CJH_CREATE_DAO_URL)
35     private String daoUrl;
36
37     /**
38      * 实现ApplicationContextAware接口的回调方法,设置上下文环境
39      */
40     @Override
41     public void setApplicationContext(ApplicationContext applicationContext) {
42         ConfigUtil.applicationContext = applicationContext;
43     }
44
45     /**
46      * 获得spring上下文
47      *
48      * @return ApplicationContext spring上下文
49      */
50     public ApplicationContext getApplicationContext() {
51         return applicationContext;
52     }
53
54     /**
55      * 获取bean
56      *
57      * @param name
58      *            service注解方式name为小驼峰格式
59      * @return Object bean的实例对象
60      */
61     public Object getBean(String name) throws BeansException {
62         return applicationContext.getBean(name);
63     }
64
65     private void initProperties() {
66
67         properties = new Properties();
68         try {
69             String[] postProcessorNames = applicationContext.getBeanNamesForType(BeanFactoryPostProcessor.class, true,
70                     true);
71             for (String ppName : postProcessorNames) {
72                 BeanFactoryPostProcessor beanProcessor = applicationContext.getBean(ppName,
73                         BeanFactoryPostProcessor.class);
74                 if (beanProcessor instanceof PropertyResourceConfigurer) {
75                     PropertyResourceConfigurer propertyResourceConfigurer = (PropertyResourceConfigurer) beanProcessor;
76                     Method mergeProperties = PropertiesLoaderSupport.class.getDeclaredMethod("mergeProperties");
77                     mergeProperties.setAccessible(true);
78                     Properties props = (Properties) mergeProperties.invoke(propertyResourceConfigurer);
79
80                     // get the method convertProperties
81                     // in class PropertyResourceConfigurer
82                     Method convertProperties = PropertyResourceConfigurer.class.getDeclaredMethod("convertProperties",
83                             Properties.class);
84                     // convert properties
85                     convertProperties.setAccessible(true);
86                     convertProperties.invoke(propertyResourceConfigurer, props);
87
88                     properties.putAll(props);
89                 }
90             }
91
92         } catch (Exception e) {
93             throw new RuntimeException(e);
94         }
95     }
96
97
98     public static Properties getProperties() {
99         return properties;
100     }
101
102     public static void setProperties(Properties properties) {
103         ConfigUtil.properties = properties;
104     }
105
106     public String getModelPack() {
107         return modelPack;
108     }
109
110     public void setModelPack(String modelPack) {
111         this.modelPack = modelPack;
112     }
113
114     public Boolean getIsScan() {
115         return "true".equals(isScan);
116     }
117
118     public void setIsScan(String isScan) {
119         this.isScan = isScan;
120     }
121
122     public String getPackFileName() {
123         return packFileName;
124     }
125
126     public void setPackFileName(String packFileName) {
127         this.packFileName = packFileName;
128     }
129
130     public String getXmlUrl() {
131         return xmlUrl;
132     }
133
134     public void setXmlUrl(String xmlUrl) {
135         this.xmlUrl = xmlUrl;
136     }
137
138     public String getDaoUrl() {
139         return daoUrl;
140     }
141
142     public void setDaoUrl(String daoUrl) {
143         this.daoUrl = daoUrl;
144     }
145 }