spring 框架, 加载配置文件、加载静态变量配置、初始化静态变量

一:第一个种利用spring框架的初始化方法,讲普通变量转变为静态变量

@Service
public class StaticVariableDemoService implements InitializingBean{
    private static String staticVariableDemo;
    
    @Value("${static.variable.demo:www}")
    private String variableDemo;
    
    private void init(){
        // 将成员变量赋值给静态变量
        staticVariableDemo = variableDemo;
    }
}

二:第二种手动使用文件读取工具,根据路径读取配置文件

1:普通变量的属性变量加载 

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

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** 
 * @ClassName PropertyLoader  
 * @Description 属性配置文件加载类 - 非单例、非线程安全, 在需要的地方创建一个
 * @author weiw
 * @date 2018年5月15日  
 *   
 */
public class PropertyLoader {
    private static final Logger logger = LoggerFactory.getLogger(PropertyLoader.class);

    private Properties configuration = null;
    
    public PropertyLoader(String configPath) {
        configuration = load(configPath);
    }

    private Properties load(String configPath) {
        Properties prop = null;
        if (StringUtils.isBlank(configPath)) {
            throw new IllegalArgumentException("配置文件路径不能为空!configPath=".concat(configPath));
        }

        try {
            InputStream is = PropertyLoader.class.getClassLoader().getResourceAsStream(configPath);
            prop = new Properties();
            prop.load(is);
            is.close();
            logger.info("Loading {} success.", configPath);
        } catch (Exception e) {
            logger.error("Loading {} failed. error:{}", configPath, e);
        }
        return prop;
    }
    
    public String getValue(String key) {
        return this.configuration.getProperty(key);
    }

    public int getIntValue(String key) {
        String valueStr = this.configuration.getProperty(key);
        try {
            return Integer.parseInt(valueStr);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return -1;
    }

    public int getIntValue(String key, int defaultValue) {
        String valueStr = this.configuration.getProperty(key);
        if (StringUtils.isEmpty(valueStr))
            return defaultValue;
        try {
            return Integer.parseInt(valueStr);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return defaultValue;
    }

    public double getDoubleValue(String key) {
        String valueStr = this.configuration.getProperty(key);
        try {
            return Double.parseDouble(valueStr);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return -1.0D;
    }

    public long getLongValue(String key) {
        String valueStr = this.configuration.getProperty(key);
        try {
            return Long.parseLong(valueStr);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return -1L;
    }
}

2:加载静态变量配置,引用PropertyLoader


/** 
 * @ClassName LoadStaticProperties  
 * @Description 初始化配置的静态常量 
 * @author weiw 
 * @date 2018年6月25日  
 *   
 */
public class LoadStaticProperties {
    private static final Logger logger = LoggerFactory.getLogger(LoadStaticProperties.class);
    private static final PropertyLoader CONFIGER =new PropertyLoader("/config/imgUtils.properties");
    private static final String fastDfsHost;
    private static final String accessKeyID;
    private static final String accessKeySecret;
    
    /** 初始化静态变量 **/
    static {
        fastDfsHost = CONFIGER.getValue("imageUtil.fast.DfsHost");
        accessKeyID = CONFIGER.getValue("imageUtil.accessKeyID");
        accessKeySecret = CONFIGER.getValue("imageUtil.accessKeySecret");
        logger.info("fastDfsHost:{}", fastDfsHost);
        logger.info("accessKeyID:{}", accessKeyID);
        logger.info("accessKeySecret:{}", accessKeySecret);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值