ViontechPropertyPlaceholderConfigurer.java
2.86 KB
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
package com.viontech.domain;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;
import java.util.Enumeration;
import java.util.Properties;
@Configuration
@Component(value = "viontechPropertyConfigurer")
public class ViontechPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
    private final String KEY =  "VIONTECH";
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        try {
            localOverride =true;
            YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            yaml.setResources(new PathMatchingResourcePatternResolver().getResources("classpath*:*.yml"));
            this.setProperties(yaml.getObject());
            this.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:*.properties"));
            //this.setLocations(new  ClassPathResource("special.properties").exists()?new  ClassPathResource("special.properties"):null,
             //       new ClassPathResource("jdbc.properties").exists()?new ClassPathResource("jdbc.properties") : null,
             //       new ClassPathResource("application.properties").exists()?new ClassPathResource("application.properties"):null);
            super.postProcessBeanFactory(beanFactory);
        }
        catch (Exception ex) {
            throw new BeanInitializationException("Could not load properties", ex);
        }
    }
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        Enumeration<?> keys = props.propertyNames();
        while (keys.hasMoreElements()) {
            String key = (String)keys.nextElement();
            String value = props.getProperty(key);
            if (null != value && value.startsWith(KEY)) {
                String valus[] = value.trim().split(KEY);
                value = valus.length >= 2?valus[1]:null;
                System.out.println(value);
                try {
                    value = ViontechAESUtil.getDecryptString(value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                props.setProperty(key, value);
            }
            if(value != null){
                System.setProperty(key, value);
            }
        }
        super.processProperties(beanFactoryToProcess, props);
    }
}