装载Properties资源文件的项目中使用
ssm项目中打算将发短信的每小时每天的限定变成可配置的。于是将配置信息写在资源文件中,现在有两种方式加载资源文件,一个是使用spring注入方式,@Value注解注入,当然,前面需要在项目中装载。第二种使用的是Properties类装载properties文件,然后获取。
这个我有点笨,spring装载失败了,所以使用第二种方式。第二种方式获取又恶心到了我,就是关于资源文件地址我有点拿不定,还有就是何时进行初始化,如何自动调用,想法是静态代码块,这个时候你需要注意static块的装载顺序。我用得不灵活,然后看了下公司的工具类,感觉挺厉害的,学会抽象封装,造轮子,耶耶耶!
代码:
package com.ref.sms.util.constant; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set; /**
* 常量配置类,从指定文件读取常量
*
*/
public class ConfigurableContants { private static final Logger logger = LoggerFactory.getLogger(ConfigurableContants.class); protected static Properties p = new Properties(); protected static String propertyFilePath = null; protected static void init(String propertyFileName) {
InputStream in = null;
try {
in = ConfigurableContants.class.getResourceAsStream(propertyFileName);
if (in != null)
p.load(in);
} catch (IOException e) {
logger.error("load " + propertyFileName + " into Contants error");
} finally {
if (in != null) {
try {
propertyFilePath = propertyFileName;
in.close();
} catch (IOException e) {
}
}
}
} protected static String getProperty(String key, String defaultValue) {
return p.getProperty(key, defaultValue);
} protected static final String getProperty(String key) {
return getProperty(key, "");
} /**
* 取以","分割的集合属性
*
* @param key
* @param defaultValue
* @return
*/
protected static Set<String> getSetProperty(String key, String defaultValue) { String[] strings = p.getProperty(key, defaultValue).split(",");
HashSet<String> hashSet = new HashSet<String>(strings.length);
for (String string : strings) {
hashSet.add(string.trim());
}
return hashSet;
} protected static Set<String> getSetProperty(String key) {
return getSetProperty(key, "");
} protected static void refreshInit(){
init(propertyFilePath);
} } package com.ref.sms.util.constant; /**
* Created by zhen on 2017-07-17.
*/
public class SmsConstants extends ConfigurableContants{ static{
init("/smsConfiguration.properties");
} public static final String SMS_DAY_LIMIT = getProperty("sms_day_limit");
public static final String SMS_HOUR_LIMIT = getProperty("sms_hour_limit"); }
使用的是Class类的getResourceAsStream方法获取资源。
public InputStream getResourceAsStream(String name)
- 查找具有给定名称的资源。查找与给定类相关的资源的规则是通过定义类的 class loader 实现的。此方法委托此对象的类加载器。如果此对象通过引导类加载器加载,则此方法将委托给
ClassLoader.getSystemResourceAsStream(java.lang.String)。在委托前,使用下面的算法从给定的资源名构造一个绝对资源名:
- 如果 name 以 '/' 开始 ('\u002f'),则绝对资源名是 '/' 后面的 name 的一部分。
- 否则,绝对名具有以下形式:
modified_package_name/name
其中 modified_package_name 是此对象的包名,该名用 '/' 取代了 '.' ('\u002e')。
-
-
- 参数:
name- 所需资源的名称- 返回:
- 一个
InputStream对象;如果找不到带有该名称的资源,则返回 null - 抛出:
NullPointerException- 如果 name 是 null
装载Properties资源文件的项目中使用的更多相关文章
- java中根据key获取resource下properties资源文件中对应的参数
properties资源文件是放在resource目录下的: 新建工具类: package com.demo.utils; import java.io.InputStream; import jav ...
- 读取web工程中.properties资源文件的模板代码
读取web工程中.properties资源文件的模板代码 // 读取web工程中.properties资源文件的模板代码 private void test2() throws IOException ...
- 在服务端中,读取properties资源文件中的数据
1.获取到资源的路径 2.读取数据 //properties文件对象 Properties properties = new Properties(); //通过HttpServletRequest ...
- Spring3.x 获取properties资源文件的值
Spring3.x 获取properties资源文件的值有两种方式: 第一种:使用<context:property-placeholder />标签 <context:prop ...
- C#.NET常见问题(FAQ)-如何把资源嵌入到项目中
首先把图像的资源添加到项目中, 选择资源文件(常规),修改一下这个资源的名字(比如叫做ButtonPic) 我们假定已经把图像放到了项目的某个文件夹下(比如Pic文件夹下,注意不是bin目录下 ...
- Eclipse中定位当前文件在项目中的位置
点击红色框内的按钮,就能定位当前文件在项目中的位置, 另外, 找到位置后记得再点击一下这个按钮, 要不然每次打开一个文件都会自动定位
- 关于properties文件在项目中的使用
这个是当时在学习JDBC的时候老师给讲的.web项目中把一些常用的用户名和密码都填写到一个对应的配置文件中,这样每次修改密码或者用户名的时候就可以直接修改这个配置文件了,不用动源码. 老师讲了两种读取 ...
- maven 打包时动态替换properties资源文件中的配置值
pom build节点下面添加resource配置: <resources> <resource> <directory>src/main/resources/&l ...
- java基础知识3--如何获取资源文件(Java中获取资源文件的url)
java开发中,常见的resource文件有:.xml,.properties,.txt文件等,后台开发中经常用到读取资源文件,处理业务逻辑,然后返回结果. 获取资源文件的方法说明getResourc ...
随机推荐
- 使用dockerfile 创建ubuntu ssh镜像
############################################################ # Dockerfile to build ubunto ssh contai ...
- mysql 数据操作 单表查询 concat_ws() 定义显示格式
有个需求用concat以这种格式打印查询 mysql> select concat(name,':',age) from employee; +----------------------+ | ...
- mysql 数据操作 多表查询 子查询 带EXISTS关键字的子查询
带EXISTS关键字的子查询 EXISTS关字键字表示存在. EXISTS 判断某个sql语句的有没有查到结果 有就返回真 true 否则返回假 False 如果条件成立 返回另外一条sql语句的返 ...
- postman:模拟发送一个需要cookie认证的请求
1.chrome 已安装插件intercept. 由于chrome安全的限制,发不出带cookie的请求.如果想要发送带cookie的请求,需要开启Interceptor. 2.chrome 浏览器要 ...
- Python之迭代器及生成器
一. 迭代器 1.1 什么是可迭代对象 字符串.列表.元组.字典.集合 都可以被for循环,说明他们都是可迭代的. 我们怎么来证明这一点呢? from collections import Itera ...
- Redis持久化磁盘IO方式及其带来的问题
有Redis线上运维经验的人会发现Redis在物理内存使用比较多,但还没有超过实际物理内存总容量时就会发生不稳定甚至崩溃的问题 一.对Redis持久化的探讨与理解 redis是一个支持持久化的内存数据 ...
- Django组件拾忆
知识预览 一 Django的form组件 二 Django的model form组件 三 Django的缓存机制 四 Django的信号 五 Django的序列化 回到顶部 一 Django的form ...
- eclipse导入Java源码
eclipse导入Java源码 下载源码包(一般jdk都自带了, 我的没有) src.zip eclipse -> window -> preferences -> JAVA -&g ...
- C语言的 32个关键之和9个控制语言之关键字
auto break case char const continue default do double else enum extern float for goto ...
- uva10817 dijkstra
大白书P330 #include <iostream> #include <cstdio> #include <algorithm> #include <st ...