fwq
1 天以前 1dd767dcc9919e3a0068835cd0dbca57f94debd5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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;
    }
}