本文为博主原创,未经允许不得转载:

在项目的应用中,经常将一些配置放入properties文件中,在代码应用中读取properties文件,就需要专门的类Properties类,通过这个类可以进行读取。

深入理解和学习的参考的详见:深入理解和学习Properties参考

此处展现在项目中读取properties配置文件中的帮助类,代码可以直接使用:

*******注:读取properties文件中的属性也可以用spring  boot中的注解来读取,可参考我的标签中spring boot中如何快速获取properties中的配置属性值

import java.io.IOException;
import java.util.Properties; public class PropertiesUtil
{ public static final String FILE_PATH = "properties/upload.properties"; //通过传入的路径及key,获得对应的值
public static String getValue(String path, String key)
{
Properties properties = new Properties();
try
{
properties.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(path));
}
catch (IOException e)
{
throw new RuntimeException("File Read Failed...", e);
}
return properties.getProperty(key);
}
//通过key直接获取对应的值
public static String getValue(String key)
{
Properties properties = new Properties();
try
{
properties.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(FILE_PATH));
}
catch (IOException e)
{
throw new RuntimeException("File Read Failed...", e);
}
return properties.getProperty(key);
} }

另外还需要在spring配置文件中,对属性文件在项目启动的时候进行初始化加载和解析:代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="configHelper" class="com.allcam.system.utils.ConfigHelper"
init-method="init"> <!--进行初始化加载-->
</bean> </beans>

java中Properties类及读取properties中属性值的更多相关文章

  1. Java的Properties类和读取.properties文件

    一..properties文件的作用 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必 ...

  2. Properties类与读取properties文件

    Properties类 在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释. 这个类的几个常 ...

  3. 通过java.util.Properties类来读取.properties文件中key对应的value

    转:http://www.cnblogs.com/panjun-Donet/archive/2009/07/17/1525597.html

  4. Java并发工具类CountDownLatch源码中的例子

    Java并发工具类CountDownLatch源码中的例子 实例一 原文描述 /** * <p><b>Sample usage:</b> Here is a pai ...

  5. 使用java中的反射获得object对象的属性值

    知识点:使用java中的反射获得object对象的属性值 一:场景 这两天开发代码时,调用别人的后台接口,返回值为Object对象(json形式的),我想获得object中指定的属性值,没有对应的ge ...

  6. PHP中使用DOM读取解析XML属性值一例

    先看XML文件结构,与常见的文件略有不同,数据并不是用闭合标签保存的,而是直接保存在属性值中. <?xml version="1.0" encoding="utf- ...

  7. Java判断一个类里是否存在某个属性

    Java判断一个类里是否存在某个属性 测试pojo类,比方我有个User类 @Getter @Setter public class User { private Long id; private S ...

  8. Java实现动态加载读取properties文件

    问题: 当我们使用如下语句加载.properties时: ClassLoader classLoader = this.getClass().getClassLoader(); Properties ...

  9. ES6中的类继承和ES5中的继承模式详解

    1.ES5中的继承模式 我们先看ES5中的继承. 既然要实现继承,首先我们得要有一个父类. Animal.prototype.eat = function(food) { console.log(th ...

随机推荐

  1. GZIPOutputStream GZIPInputStream

    GZIP is appropriate for single data stream. Example: Compress one file public class Demo8 {  public ...

  2. awk和sed

    在正则表达式那章有两个比较好用的命令sed 和awk awk 是按指定分隔符分隔,默认空白键,第一个为$1,第二个为$2... 命令: FS分隔符

  3. Hive分区表新增字段及修改表名,列名,列注释,表注释,增加列,调整列顺序,属性名等操作

    一.Hive分区表新增字段 参考博客:https://blog.csdn.net/yeweiouyang/article/details/44851459 二.Hive修改表名,列名,列注释,表注释, ...

  4. Hive的join表连接查询的一些注意事项

    Hive支持的表连接查询的语法: join_table: table_reference JOIN table_factor [join_condition] | table_reference {L ...

  5. Spark With Mongodb 实现方法及error code -5, 6, 13127解决方案

    1.spark mongo 读取 val rdd = MongoSpark.builder().sparkSession(spark).pipeline(Seq(`match`(regex(" ...

  6. android studio 添加get,set方法快捷方式

    android studio 添加get,set方法快捷方式

  7. Q_DECL_OVERRIDE

    Q_DECL_OVERRIDE也就是c++的override # define Q_DECL_OVERRIDE override 在重写虚函数时会用到, 作用是防止写错虚函数: void keyPre ...

  8. django实现类似触发器的效果

    https://blog.csdn.net/pushiqiang/article/details/50652080?utm_source=blogxgwz1 https://blog.csdn.net ...

  9. jQuery知识总结(转)

    原文:http://fwhyy.com/2013/04/jquery-knowledge-summary/ 这篇文章在于筛选器的简单例子,让人一看就懂代码的作用 20170223 前言 jQuery一 ...

  10. centos6.5安装部署zabbix监控服务端和客户端

    部署zabbix服务端需要LNMP环境(nginx,mysql,php),其它数据库也可以,我这里使用mysql,关于LNMP环境部署,可以参考我的另一遍文章:http://www.cnblogs.c ...