装载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 - 如果 namenull
原文地址:https://www.cnblogs.com/aigeileshei/p/7198808.html