您的位置:首页 > 编程语言 > Java开发

Spring 将properties文件转为系统工具类

2015-10-22 19:01 519 查看
context配置
<bean id="configBean" class="com.morningstar.productservices.search.ac.common.frontend.common.util.CommonPropertyConfigurer">
<property name="location">
<value>classpath:idservice.${env}.${site}.properties</value>
</property>
</bean>



实现类

package xxx.common.frontend.common.util;

import java.util.Properties;

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

/**
* @author Haber He (hhe5)
*
*/
public class CommonPropertyConfigurer extends PropertyPlaceholderConfigurer {

private static Properties properties;

@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {
super.processProperties(beanFactory, props);
properties = props;
}

public static String getValue(String key) {
return properties.getProperty(key);
}

public static int getIntValue(String key) {
return Integer.parseInt(properties.getProperty(key));
}

public static boolean getBooleanValue(String key){
return Boolean.parseBoolean(properties.getProperty(key));
}
}


用法
CommonPropertyConfigurer.getValue("Cache-Control")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: