java调用properties中的配置

使用场景:使用dubbo服务,对应了dev,test,prod等环境的配置文件,如:filter-dev.properties,filter-test.properties,filter-prod.properties.不同的properties里面对于同一个变量需要有不同的配置,比如我设定了每日短信发送条数上线:sms.limit.count=20000

接手的是老代码,对应配置的调用有两种方式,
一种是在xml文件中声明一个对PropertyPlaceholderConfigurer的继承类PropertiesLoader

<bean id="propertyPlaceholderConfigurer" class="com.artist.common.utils.PropertiesLoader">
<property name="location">
<value>classpath:env/filter-${environment}.properties</value>
</property>
</bean>
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.artist.common.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class PropertiesLoader extends PropertyPlaceholderConfigurer {
private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
private static Map<String, String> propertyMap;

public PropertiesLoader() {
}

protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
propertyMap = new HashMap();
Iterator var3 = props.keySet().iterator();

while(var3.hasNext()) {
Object key = var3.next();
String keyStr = key.toString();
String value = props.getProperty(keyStr);
propertyMap.put(keyStr, value);
}

}

public static String getValue(String key) {
String systemProperty = System.getProperty(key);
return systemProperty != null?systemProperty:(String)propertyMap.get(key);
}

public String getProperty(String key) {
String value = getValue(key);
if(value == null) {
throw new NoSuchElementException();
} else {
return value;
}
}

public String getProperty(String key, String defaultValue) {
String value = getValue(key);
return value != null?value:defaultValue;
}

public static Integer getInteger(String key) {
String value = getValue(key);
if(value == null) {
throw new NoSuchElementException();
} else {
return Integer.valueOf(value);
}
}

public static Integer getInteger(String key, Integer defaultValue) {
String value = getValue(key);
return value != null?Integer.valueOf(value):defaultValue;
}

public static Double getDouble(String key) {
String value = getValue(key);
if(value == null) {
throw new NoSuchElementException();
} else {
return Double.valueOf(value);
}
}

public static Double getDouble(String key, Integer defaultValue) {
String value = getValue(key);
return Double.valueOf(value != null?Double.valueOf(value).doubleValue():(double)defaultValue.intValue());
}

public static Boolean getBoolean(String key) {
String value = getValue(key);
if(value == null) {
throw new NoSuchElementException();
} else {
return Boolean.valueOf(value);
}
}

public static Boolean getBoolean(String key, boolean defaultValue) {
String value = getValue(key);
return Boolean.valueOf(value != null?Boolean.valueOf(value).booleanValue():defaultValue);
}
}

java调用:private static String SMSLIMITCOUNT= PropertiesLoader.getValue("sms.limit.count");


一种是用spring的@Value 比如
@Value("${sms.limit.count}")
String smsLimitCount;


在xml配置文件中声明properties:
<context:property-placeholder location="classpath:config/application.properties,classpath:env/filter-${environment}.properties"/>


相比之下,第二种方法比较简单,只是之前写的人写法是这样的:
<context:property-placeholder location="classpath:config/application.properties"/>
<context:property-placeholder location="classpath:env/filter-${environment}.properties"/>


这种写法导致evn/filter-${enviroment}.properties里面的配置文件项在java中找不到,在xml配置文件里是可以的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值