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

在项目的应用中,经常将一些配置放入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. shell 编程每日100行

    xiluhua@localhost ~/tscripts $ echo "hello world !" -bash: !": event not found xiluhu ...

  2. MCMC算法深入理解

    MCMC(Markov Chain Monte Carlo),即马尔科夫链蒙特卡洛方法,是以马尔科夫平稳状态作为理论基础,蒙特卡洛方法作为手段的概率序列生成技术. MCMC理论基础 如果转移矩阵为P的 ...

  3. Java并发编程:volatile关键字解析-转

    Java并发编程:volatile关键字解析 转自海子:https://www.cnblogs.com/dayanjing/p/9954562.html volatile这个关键字可能很多朋友都听说过 ...

  4. linux 环境RPM 安装MYSQL5.6

    linux 环境RPM 安装MYSQL5.6 系统环境 CentOS7.2 1.关闭selinux 服务[SELinux是一种基于域-类型 模型(domain-type)的强制访问控制(MAC)安全系 ...

  5. [转载]oracle的加密和解密

    加密函数 create or replace function encrypt_des(p_text varchar2, p_key varchar2) return varchar2 isv_tex ...

  6. [转载]css代码优化的12个技巧

    1.ID 规则2.Class 规则3.标签规则4.通用规则对效率的普遍认识是从Steve Souders在2009年出版的<高性能网站建设进阶指南>开始,虽然该书中罗列的更加详细,但你也可 ...

  7. 关于reduce输出write方法

    关于hadoop一些自定义输出 code>OutputFormat</code> describes the output-specification for a * Map-Red ...

  8. linux 查找locate find

    1.locate locate指令和find找寻档案的功能类似,但locate是透过update程序将硬盘中的所有档案和目录资料先建立一个索引数据库,在 执行loacte时直接找该索引,查询速度会较快 ...

  9. JDK常用命令(三)jmap

    jmap jmap,Java Memory Map.主要用于打印指定Java进程(或核心文件.远程调试服务器)的共享对象内存映射或堆内存细节. jmap命令可以获得运行中的jvm的堆的快照,从而可以离 ...

  10. Java中断异常 InterruptedException 的正确处理方式

    你看到这篇文件可能是因为你已经调用了一个抛出 InterruptedException 异常的方法,并且需要以某种方式处理它. 首先,需要了解为一个方法为啥会 throws InterruptedEx ...