ViontechPropertyPlaceholderConfigurer.java 2.86 KB
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);
    }
}