spring 中读取properties 文件
2014-09-29 11:08
274 查看
在src 目录下建立configs.properties
建立静态类:
然后配置静态类(详见我另一篇 http://www.cnblogs.com/yqweber/p/3992513.html) 就能在页面直接用来取值了.
backup.host = 192.168.1.6 backup.user = root backup.pwd =pwd
建立静态类:
package com.ly.jxc.util; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; /** * 配置文件读取类 * @author yq * 2014-09-29 */ public class Configs extends PropertyPlaceholderConfigurer { private static Map<String, Object> ctxPropertiesMap; protected static void load(){ Resource resource = new ClassPathResource("/config.properties"); Properties props; try { props = PropertiesLoaderUtils.loadProperties(resource); ctxPropertiesMap = new HashMap<String, Object>(); for (Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 返回int,带默认值 * @param name * @return */ public static int getIntValue(String name,int defaultValue) { if(ctxPropertiesMap ==null ||ctxPropertiesMap.isEmpty()) load(); if(ctxPropertiesMap.get(name)==null) return defaultValue; return Integer.parseInt((String)ctxPropertiesMap.get(name)); } /** * 返回int * @param name * @return */ public static int getIntValue(String name) { if(ctxPropertiesMap ==null ||ctxPropertiesMap.isEmpty()) load(); if(ctxPropertiesMap.get(name)==null) return 0; return Integer.parseInt((String)ctxPropertiesMap.get(name)); } /** * 返回string * @param name * @return */ public static String getValue(String name) { if(ctxPropertiesMap ==null || ctxPropertiesMap.isEmpty()) load(); return (String)ctxPropertiesMap.get(name); } }
然后配置静态类(详见我另一篇 http://www.cnblogs.com/yqweber/p/3992513.html) 就能在页面直接用来取值了.
相关文章推荐
- Spring读取数据连接属性文件properties(单个和多个)
- Spring 使用配置文件读取jdbc.properties
- Spring使用程序方式读取properties文件
- Spring中配置和读取多个Properties文件
- Spring使用配置文件读取jdbc.properties
- SPRING使用占位符读取字段加密的properties文件
- spring读取properties和xml配置文件的方法
- Spring中属性文件properties的读取与使用
- Spring使用配置文件读取jdbc.properties
- Spring中配置和读取多个Properties文件--转
- Spring 如何读取properties文件内容
- Spring读取自定义properties配置文件
- spring 读取properties配置文件
- Spring中属性文件properties的读取与使用
- Spring读取数据连接属性文件properties(单个和多个)
- spring 框架的xml文件如何读取properties文件数据
- 如何在spring中读取properties配置文件里面的信息
- spring读取properties属性文件,并替换占位符中的值.
- spring读取properties文件信息
- Spring中配置和读取多个Properties文件