默认加载全部配置文件*.properties

package com.pds.PropertiesLoad;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.Properties;

/**
 * Created with IDEA
   ssm项目配置在beans.xml中可以通过${}的方式获取其中的属相值
 加载配置属性文件
 <bean id="propertyLoad" class="com.pds.PropertiesLoad.PropertyLoad">
  定义默认数据源配置, 使用 Druid 数据库连接池 
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
 init-method="init" destroy-method="close">
 < 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass 
 <property name="driverClassName" value="${jdbc.driver}"/>
 </bean>
 * @author yunfei.zhao
 * @Date:2019/7/23
 * @Time:11:03
 */
public class PropertyLoad extends PropertyPlaceholderConfigurer {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        Properties properties = DefaultContext.getProperties();
        setProperties(properties);
        super.postProcessBeanFactory(beanFactory);
    }
}

package com.pds.PropertiesLoad;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.thymeleaf.util.Validate;

import java.util.Map;
import java.util.Properties;

/**
 * Created with IDEA
 *  多文件加载
 * @author yunfei.zhao
 * @Date:2019/7/23
 * @Time:11:05
 */
public class DefaultContext {
    /**
     * 当前实例对象
     */
     private static DefaultContext CONFUG =  new DefaultContext();
    /**
     * 保存全局属性值
     */
     private static Map<String,String> map = Maps.newHashMap();
    /**
     * 属性文件加载对象
     */
    private  static PropertiesLoader loader;
    /**
     * 上传文件基础虚拟路径
     */
    public static final String USER_DIR = "user.dir";

    private static boolean inited = false;

    private static String path = "classpath*:/conf*/**/*.properties";

    /**
     * 初始化配置文件
     */
    public static void init(){
        if(!inited){
            map = Maps.newHashMap();
            loader = new PropertiesLoader(path);
            inited = true;
        }
    }
    /**
     * 获取当前对象实例
     * @return
     */
    public static DefaultContext getInstance(){
       return CONFUG;
    }

    /**
     * 获取配置
     * @param key
     * @return
     */
    public static String get(String key){
        init();
        Validate.notNull(map,"");
        String value = map.get(key);
        if(value == null){
            Validate.notNull(loader,"");
            value = loader.getProperty(key);
            map.put(key,value!=null?value: StringUtils.EMPTY);
        }
        return value;
    }
    public static Properties getProperties(){
        init();
        return DefaultContext.loader.getProperties();
    }
}

package com.pds.PropertiesLoad;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;
import java.util.Properties;

/**
 * Created with IDEA
 * Properties文件载入工具类. 可载入多个properties文件,
 * 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.
 * @author yunfei.zhao
 * @Date:2019/7/23
 * @Time:11:13
 */
public class PropertiesLoader {
    private static ResourceLoader resourceLoader;
    private final Properties properties;

    public PropertiesLoader(String...  resourcePaths) {
        resourceLoader = new DefaultResourceLoader();
        properties =loadProperties(resourcePaths);
    }

    public Properties getProperties() {
        return properties;
    }

    /**
     * 取出Property,但以System的Property优先,取不到返回空字符串.
     * @param value
     * @return
     */
    private String getValue(String value){
        String systemProperty = System.getProperty(value);
        if(systemProperty!=null){
            return systemProperty;
        }
        if(properties.containsKey(value)){
            return properties.getProperty(value);
        }
        return "";
    }

    /**
     * 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.
     * @param key
     * @return
     */
    public String getProperty(String key){
        String value = getValue(key);
        if(value == null){
            throw new NoSuchElementException();
        }
        return value;
    }

    /**
     * 取出String类型的Property,但以System的Property优先.如果都为Null则返回Default值.
     * @param key
     * @param def
     * @return
     */
    public String getProperty(String key,String def){
        String value = getValue(key);
        return value != null ? value : def;
    }
    /**
     * 载入多个文件, 文件路径使用Spring Resource格式.
     */
    private Properties loadProperties(String... resourcesPaths){
        Properties pros = new Properties();
        for(String location : resourcesPaths){
            InputStream is = null;
            ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
            Resource[] resourceArr;
            try {
                for (Resource resource : resourceArr = patternResolver.getResources(location)) {
                     try {
                         is = resource.getInputStream();
                         pros.load(is);
                     }catch (IOException io){

                     }finally {
                         IOUtils.closeQuietly(is);
                     }

                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return pros;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值