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 - 如果 namenull

装载Properties资源文件的项目中使用的更多相关文章

  1. java中根据key获取resource下properties资源文件中对应的参数

    properties资源文件是放在resource目录下的: 新建工具类: package com.demo.utils; import java.io.InputStream; import jav ...

  2. 读取web工程中.properties资源文件的模板代码

    读取web工程中.properties资源文件的模板代码 // 读取web工程中.properties资源文件的模板代码 private void test2() throws IOException ...

  3. 在服务端中,读取properties资源文件中的数据

    1.获取到资源的路径 2.读取数据 //properties文件对象 Properties properties = new Properties(); //通过HttpServletRequest ...

  4. Spring3.x 获取properties资源文件的值

    Spring3.x 获取properties资源文件的值有两种方式:  第一种:使用<context:property-placeholder />标签  <context:prop ...

  5. C#.NET常见问题(FAQ)-如何把资源嵌入到项目中

    首先把图像的资源添加到项目中,   选择资源文件(常规),修改一下这个资源的名字(比如叫做ButtonPic)   我们假定已经把图像放到了项目的某个文件夹下(比如Pic文件夹下,注意不是bin目录下 ...

  6. Eclipse中定位当前文件在项目中的位置

    点击红色框内的按钮,就能定位当前文件在项目中的位置, 另外, 找到位置后记得再点击一下这个按钮, 要不然每次打开一个文件都会自动定位

  7. 关于properties文件在项目中的使用

    这个是当时在学习JDBC的时候老师给讲的.web项目中把一些常用的用户名和密码都填写到一个对应的配置文件中,这样每次修改密码或者用户名的时候就可以直接修改这个配置文件了,不用动源码. 老师讲了两种读取 ...

  8. maven 打包时动态替换properties资源文件中的配置值

    pom build节点下面添加resource配置: <resources> <resource> <directory>src/main/resources/&l ...

  9. java基础知识3--如何获取资源文件(Java中获取资源文件的url)

    java开发中,常见的resource文件有:.xml,.properties,.txt文件等,后台开发中经常用到读取资源文件,处理业务逻辑,然后返回结果. 获取资源文件的方法说明getResourc ...

随机推荐

  1. spring自定义事务同步器(二):借助redisson实现自己的同步器

    1. 借助redis的java客户端redisson实现自己的事物同步器 @Override public void lockWithinCurrentTransaction(Object key) ...

  2. C#基础整理(二)

    1.变量类型int.double.string.char.bool.decimal变量使用规则:先声明,再赋值,最后使用 2.命名规范:Camel:第一个单词首字母小写,其他单词首字母大写,其余字母小 ...

  3. 004-ant design -dispatch、request、fetch

    一.dispatch 函数 typedispatch = (a: Action) => Action dispatching function 是一个用于触发 action 的函数,action ...

  4. PhotoSwipe中文API(二)

    配置 选项是在键 - 值对添加作为参数传递给PhotoSwipe构造,例如通过: var options = { index: 3, escKey: false, // ui option timeT ...

  5. vue学习之一vue初识

    一.vue.js是什么 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层 ...

  6. Django小项目简单BBS论坛

    开发一个简单的BBS论坛 项目需求: 1 整体参考"抽屉新热榜" + "虎嗅网" 2 实现不同论坛版块 3 帖子列表展示 4 帖子评论数.点赞数展示 5 在线用 ...

  7. VS2010/MFC编程入门之十三(对话框:属性页对话框及相关类的介绍)

    前面讲了模态对话框和非模态对话框,本节开始鸡啄米讲一种特殊的对话框--属性页对话框.另外,本套教程所讲大部分对VC++各个版本均可适用或者稍作修改即可,但考虑到终究还是基于VS2010版本的,所以将& ...

  8. Ubuntu软件包管理器

    Ubuntu软件包管理 Ubuntu下对软件管理工具有:apt,dpkg,tasksel,aptitude等,我们常用的就是前三个工具.下面就介绍这三个工具的用法. dpkg 在Linux发展之初,安 ...

  9. Java传统下载和SpringMVC下载

    /** * 传统下载 * @param filename 文件名称 */ @RequestMapping(value="download", method={RequestMeth ...

  10. Migrating from Spring 3 to Spring 4 - org.springframework.scheduling.quartz.CronTriggerBean

    I'm trying to migrate from spring 3.0.5 to spring 4.1.X . Spring 3 has Class named as "org.spri ...