`

ConfigUtil(配置文件加载操作)

    博客分类:
  • java
 
阅读更多

 

纯代码

第一步:配置好配置文件:

 

配置文件置于 框架的 src 下,命名为 application.properties

内容类似于:

Xml代码  收藏代码
  1. #oracle version database settings  
  2. #jdbc.driver=oracle.jdbc.driver.OracleDriver  
  3. #jdbcjdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:space  
  4. #jdbc.username=space  
  5. #jdbc.password=space  
  6. #hibernate.dialect=org.hibernate.dialect.Oracle10gDialect  
  7. ###本应用数据库########################################################################  
  8. jdbc.driver=com.mysql.jdbc.Driver  
  9. jdbcjdbc.url=jdbc:mysql://127.0.0.1:3306/patientfriends?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true  
  10. jdbc.username=root  
  11. jdbc.password=root  
  12. hibernate.dialect=org.hibernate.dialect.MySQLDialect  
  13. #hibernate settings  
  14. hibernate.show_sql=true  
  15. hibernate.format_sql=false  
  16. #dbcp settings  
  17. dbcp.maxIdle=5  
  18. dbcp.maxActive=40  

 

里面存放框架中所有应该提取出的参数信息,比如:数据库连接信息,定时器时间戳,Email相关参数信息,图片宽高参数,URl,用户名密码等等。

 

第二步:编写 配置文件加载操作类 ConfigUtil

Java代码  收藏代码
  1. public class ConfigUtil {  
  2.       
  3.     private static PropertiesConfiguration  config=null;  
  4.     static{  
  5.         try{  
  6.             //config = new PropertiesConfiguration("app.properties");  
  7.             //config.setEncoding("UTF-8");  
  8.             //config.setHeader(header)  
  9.             config = new PropertiesConfiguration();  
  10.             config.setEncoding("UTF-8");  
  11.             config.load("application.properties");  
  12.               
  13.         }catch(Exception ex){  
  14.         }  
  15.     }  
  16.       
  17.       
  18.     public static int getIntValue(String key){  
  19.         int reInt = 1;  
  20.         try{  
  21.             //PropertiesConfiguration  config = new PropertiesConfiguration("conf.properties");  
  22.             reInt = config.getInt(key);  
  23.         }catch(Exception ex){  
  24.             ex.fillInStackTrace();  
  25.             reInt = 0;  
  26.         }  
  27.         return reInt;  
  28.     }     
  29.       
  30.     public static Long getLongValue(String key) {  
  31.         Long reLong = 1l;  
  32.         try{  
  33.             //PropertiesConfiguration  config = new PropertiesConfiguration("conf.properties");  
  34.             reLong = config.getLong(key);  
  35.         }catch(Exception ex){  
  36.             ex.fillInStackTrace();  
  37.             reLong = 0l;  
  38.         }  
  39.         return reLong;  
  40.     }  
  41.       
  42.       
  43.       
  44.     public static double getDoubleValue(String key){  
  45.         double reDouble = 1.0;  
  46.         try{  
  47.             //PropertiesConfiguration  config = new PropertiesConfiguration("conf.properties");  
  48.             reDouble = config.getDouble(key);  
  49.         }catch(Exception ex){  
  50.             ex.fillInStackTrace();  
  51.             reDouble =1.0;  
  52.         }  
  53.         return reDouble;  
  54.     }  
  55.       
  56.     public static String getStringValue(String key){  
  57.         String str = "";  
  58.         try{  
  59.             //PropertiesConfiguration  config = new PropertiesConfiguration("conf.properties");  
  60.             str = config.getString(key);  
  61.         }catch(Exception ex){  
  62.             ex.fillInStackTrace();  
  63.             str = "";  
  64.         }  
  65.         return str;  
  66.     }  
  67.       
  68.     public static Boolean getBooleanValue(String key) {  
  69.         Boolean flag = false;  
  70.         try{  
  71.             flag = config.getBoolean(key);  
  72.         }catch(Exception ex){  
  73.             ex.fillInStackTrace();  
  74.         }  
  75.         return flag;  
  76.     }  
  77.       
  78.     public static void save(String key,Object o){  
  79.         config.setProperty(key, o);  
  80.         try{  
  81.             //config.save("score.properties");  
  82.             config.save("application.properties");  
  83.             //config = new PropertiesConfiguration("app.properties");  
  84.             config = new PropertiesConfiguration();  
  85.             config.setEncoding("UTF-8");  
  86.             config.load("application.properties");  
  87.         }catch(Exception ex){  
  88.             ex.printStackTrace();  
  89.         }  
  90.     }  
  91.       
  92.     public static void main(String[] args){  
  93.         System.out.println("ewew");  
  94.         System.out.println(getIntValue("vip.money"));  
  95.         //ConfigUtil.save("rmb.money", 20);  
  96.         //ConfigUtil.save("month.live", "123334");  
  97.         System.out.println(ConfigUtil.getStringValue("month.live1"));  
  98.         //System.out.println(getStringValue("month.live"));  
  99.     }  
  100.       
  101.     // 读取配置文件  
  102.     private static ResourceBundle cache = null;  
  103.   
  104.     static {  
  105.         try {  
  106.             cache = ResourceBundle.getBundle("application");  
  107.         } catch (RuntimeException e) {  
  108.             e.printStackTrace();  
  109.         }  
  110.     }  
  111.   
  112.     /** 
  113.      * 功能描述:获取配置文件参数值 
  114.      *  
  115.      * @param str(参数KEY值) 
  116.      * @return 
  117.      */  
  118.     public static String getValue(String str) {  
  119.         String s = cache.getString(str);  
  120.         try {  
  121.             s = new String(s.getBytes("ISO8859-1"), "utf-8");  
  122.         } catch (UnsupportedEncodingException e) {  
  123.             e.printStackTrace();  
  124.         }  
  125.         return s;  
  126.     }  
  127.   
  128.     /** 
  129.      * 功能描述:获取指定配置文件参数的值 
  130.      *  
  131.      * @param strPropertiesFile(配置文件名称) 
  132.      * @param strItem(参数名称) 
  133.      * @return 
  134.      */  
  135.     public String getPropertiesValue(String strPropertiesFile, String strItem) {  
  136.         String strItemValue = "";  
  137.         ResourceBundle resources1 = null;  
  138.         try {  
  139.             resources1 = ResourceBundle.getBundle(strPropertiesFile);  
  140.             strItemValue = resources1.getString(strItem);  
  141.         } catch (MissingResourceException e) {  
  142.             System.out.println("ConfigInfo.getPropertiesValue error:"  
  143.                     + e.getMessage());  
  144.         }  
  145.         return strItemValue;  
  146.     }  
  147.       
  148.       
  149.       
  150. }  
分享到:
评论

相关推荐

    ConfigUtil.java

    读取consle配置的工具类

    Python库 | configutil-0.0.1.dev2.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:configutil-0.0.1.dev2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    可直接用的微信h5支付dem0

    修改ConfigUtil里面配置上自己的东西就可以直接测试了。具体里面有说明

    ueditor 映射项目外的物理路径

    2.用myeclipse的文件查找工具查找physicalpath ,修改physicalpath为String physicalPath = ConfigUtil.get("savepath") + savePath;,具体修改三个位置。此工程直接导入myeclipse直接可以运行,物理路径在tomcat的...

    config-util:小型,轻巧和可扩展的Node.js库,用于验证评估文件(专注于配置对象的使用)

    小型,轻巧和可扩展的Node.js库,用于验证/评估文件(侧重于配置对象的使用)。 安装 $ npm install config-util --save 特征 对象验证。 通过键路径获取和设置值。 定义用于对象验证的模板的简便方法。 编译评估...

    Android代码-看知乎安卓App

    配置信息采用专门的ConfigUtil类处理,降低耦合度; 所有异步任务采用了回调机制; 对于Json的解析交由JavaBean自己解析,降低耦合度; 采用了三级图片缓存(文件、软引用,LRU),并解决图片错位等问题,仅需调用...

    java utils 常用工具类 - 十年工作经验总结

    十年工作经验总结的java 常用工具类分享大家-ArrayUtil,CharsetUtil,CharUtil,CheckUtil,ChinesUtil,ClassUtil,ConfigUtil,ConvertUtil,DateUtil,EmailUtil,ExceptionUtil,FilePathUtil,FileUtil,...

    微信扫码支付 demo

    业务需求只做了扫码支付。...请在 ConfigUtil.java 填上对应参数 运行 WXPay.java 就可以得到扫码下单的返回字符串 https://www.helloweba.net/javascript/226.html 前端生成二维码。 生活愉快,有事留言~

    直升机停机坪

    模组引导程序为了方便的配置和启动一个Helidon se应用程序作为Boot 。 一些Config的util工具为ConfigUtil 。 可以将helidon se与其他电动工具扩展的plugin的抽象。 ari 具有数据源和连接池的plugin实现。

Global site tag (gtag.js) - Google Analytics