在开发中有时候要获取配置文件里的值,通常可以利用如下方式来读取:

public class PropertyUtil {

    private static Properties p = new Properties();

    static {
init("config.properties");
} /**
* read config file and set vlaue to Properties p
* @param propertyFileName 文件名
*/
protected static void init(String propertyFileName)
{
InputStream in = null;
try {
in = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFileName);
if (in != null){
p.load(in);
}
} catch (IOException e){
System.err.println("load ["+propertyFileName+"] error!"+e.getMessage());
} finally {
IOUtils.closeQuietly(in);
}
} public static String getValue(String key)
{
String value = null;
try {
value = p.getProperty(key);
} catch (Exception e) {
//
}
return value;
}
}

下面推荐一个利用PropertyPlaceholderConfigurer来获取配置文件元素的方法:

package com.spring.common.util;

import java.util.HashMap;
import java.util.Map;import java.util.Properties; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; public final class PropertiesUtil extends PropertyPlaceholderConfigurer { private static Map<String, String> PropertiesMap;
private static Properties properties = new Properties(); @Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
properties.putAll(props);
PropertiesMap = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = (String) key;
String value = props.getProperty(keyStr);
PropertiesMap.put(keyStr, value);
}
} /**
* Get a value based on key , if key does not exist , null is returned
* @param key
* @return
*/
public static String getString(String key) {
try {
return PropertiesMap.get(key);
} catch (Exception e) {
return null;
}
} /**
* 根据key获取值
*
* @param key
* @return
*/
public static int getInt(String key) {
return Integer.parseInt(PropertiesMap.get(key));
} /**
* 根据key获取值
*
* @param key
* @param defaultValue
* @return
*/
public static int getInt(String key, int defaultValue) {
String value = PropertiesMap.get(key);
if (StringUtils.isBlank(value)) {
return defaultValue;
}
return Integer.parseInt(value);
} public static boolean getBoolean(String key) {
return getBoolean(key, false);
} /**
* 根据key获取值
* @param key
* @param defaultValue
* @return
*/
public static boolean getBoolean(String key, boolean defaultValue) {
String value = PropertiesMap.get(key);
if (StringUtils.isBlank(value)) {
return defaultValue;
}
return Boolean.parseBoolean(value);
} }

需要在spring.xml文件里引入一下配置文件,注意类名全路径不要写错:

    <!-- 引入属性配置文件 -->
<bean class="com.spring.common.util.PropertiesUtil">
<property name="locations">
<list>
<value>classpath:config.properties</value>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>

在需要的地方调用下getString方法:

String  password = PropertiesUtil.getString("password");

获取Spring项目配置文件元素的更多相关文章

  1. Java中如何获取spring中配置文件.properties中属性值

    通过spring配置properties文件 1 2 3 4 5 6 7 8 9 <bean id="propertyConfigurer"   class="co ...

  2. 监听器如何获取Spring配置文件(加载生成Spring容器)

    Spring容器是生成Bean的工厂,我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个 ...

  3. 监听器如何获取Spring配置文件

    我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个监听器,在项目启动时将首页的数据查询出 ...

  4. Spring项目的配置文件们(web.xml context servlet springmvc)

    我们的spring项目目前用到的配置文件包括1--web.xml文件,这是java的web项目的配置文件.我理解它是servlet的配置文件,也就是说,与spring无关.即使你开发的是一个纯粹jsp ...

  5. maven Spring获取不到配置文件

    如题: 如果在maven项目中,Spring获取不到配置文件, 把配置文件放到.src/main/resource文件夹下即可 import org.springframework.context.s ...

  6. spring项目读取配置文件

    Spring项目在运用中读取配置文件有两种方式: 通过项目的配置文件读取 在spring-context.xml里面加入以下代码 在运用到的类里面加入 @Value("#{configPro ...

  7. Spring读取配置文件,获取bean的几种方式

    BeanFactory有很多实现类,通常使用 org.springframework.beans.factory.xml.XmlBeanFactory类.但对于大部分J2EE应用而言,推荐使 用App ...

  8. web项目中获取spring的bean对象

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...

  9. Spring中获取web项目的根目录

    spring 在 org.springframework.web.util 包中提供了几个特殊用途的 Servlet 监听器,正确地使用它们可以完成一些特定需求的功能; WebAppRootListe ...

随机推荐

  1. virtualenv 使用

    一.安装 pip install virtualenv 因为我已经安装了pip,那么就直接用pip来安装了,简单方便. 其它的安装方式请参考官方网站:http://www.virtualenv.org ...

  2. JS模块化编程(五)---按照AMD规范扩展全局对象

    采用AMD规范 具体来说,就是模块必须采用特定的define()函数来定义;如果一个模块不依赖其他模块,那么可以直接定义在define()函数中; 以扩展全局对象Date为例: define(func ...

  3. js数据类型--对象&数组

    javascript最重要的数据类型是对象 对象定义 对象是键值对的集合,或字符串到值映射的集合 对象申明 对象是由花括号括起来的 var person={ name:"my name&qu ...

  4. mybatis之入门

    一.mybatis介绍 是apache旗下的一个开源的顶级ORM框架(做dao层的操作) 开始叫ibatis在2010年经过升级后发布到google code上就改名为mybatis 定位:1.是一个 ...

  5. 移除wordpress版本信息 删除无用信息

    wordpress页面头部有很多无用的信息,像wordpress版本信息.feed等,如何把它们删除或不让它们先是出来呢? 将下面的代码加入到当前主题的functions.php,可以适当酌情保留 & ...

  6. spring boot 中用@value给static变量赋值

    需求:改写一个JedisUtils,工具类,所以最好用静态方法和变量. @value("${redis.host}") private static String redisHos ...

  7. 关于Python类属性与实例属性的讨论

    标题名字有点长. 之所以想写这个文章是因为碰巧看到网上一篇关于Pyhon中类属性及实例属性区别的帖子.因为我之前也被这个问题困扰过,今天碰巧看到了这篇帖子,发现帖子的作者只是描述了现象,然后对原因的解 ...

  8. test examples/test scripts

    //https://www.getpostman.com/docs/v6/postman/scripts/test_examples //Setting an environment variable ...

  9. spark shuffle原理

    1.spark中窄依赖的时候不需要shuffle,只有宽依赖的时候需要shuffle,mapreduce中map到reduce必须经过shuffle 2.spark中的shuffle fetch的时候 ...

  10. scipy模块