Jfinal 配置文件工具类

一起因:

1)最近,有个小的工具需进行数据库操作,用到Jfinal的ActiveRecord,但是发现若要从配置文件中读取参数的,Jfinal中相应的代码都在JFinalConfig中。最后只能把相关的代码抽取出来来实现配置参数的读取。

2)一个基于Jfinal的项目中有不少配置参数,个人有个喜好,就是一个配置文件只干一件事情,比如database.properties里面只存取几个数据库连接信息。router.properties里面只存储路由相关信息。个人不喜欢将所有配置都塞到一个类似app.properties的文件里。

重构目标:


1)将从配置文件中读取参数相关代码从JFinalConfig中移除,专门建立一个类来做此事,方便单独需要ActiveRecord模块的场合使用,提高灵活性。

2)支持从多个properties文件中读取配置,并能通过统一的接口来获取这些文件中的所有配置信息。

上代码:

01 public abstract class JFinalConfig {
02  
03     /**
04      * Config constant
05      */
06     public abstract void configConstant(Constants me);
07  
08     /**
09      * Config route
10      */
11     public abstract void configRoute(Routes me);
12  
13     /**
14      * Config plugin
15      */
16     public abstract void configPlugin(Plugins me);
17  
18     /**
19      * Config interceptor applied to all actions.
20      */
21     public abstract void configInterceptor(Interceptors me);
22  
23     /**
24      * Config handler
25      */
26     public abstract void configHandler(Handlers me);
27  
28     /**
29      * Call back after JFinal start
30      */
31     public void afterJFinalStart(){};
32  
33     /**
34      * Call back before JFinal stop
35      */
36     public void beforeJFinalStop(){};
37 }



新建的PropertyConfig类代码,

1)允许多文件读取
2)除了兼容JFianl从WEB-INF读取配置信息外,也允许从classpath中读取配置信息。
3)单例模式运行。

001 import java.io.File;
002 import java.io.FileInputStream;
003 import java.io.IOException;
004 import java.io.InputStream;
005 import java.util.Map.Entry;
006 import java.util.Properties;
007 import java.util.concurrent.ConcurrentHashMap;
008 import java.util.concurrent.ConcurrentMap;
009  
010 import com.jfinal.kit.PathKit;
011 import com.jfinal.kit.StringKit;
012  
013 /**
014  *
015  * PropertyConfig: read properties file
016  * file path: ../WEB-INFO/ or classpath
017  *
018  */
019 public class PropertyConfig {
020  
021     private ConcurrentMap<String, Object> properties =  new ConcurrentHashMap<String, Object>();
022  
023     private static PropertyConfig config = new PropertyConfig();
024  
025     private PropertyConfig(){}
026  
027     public static PropertyConfig me(){
028         return config;
029     }
030  
031  
032     public void loadPropertyFile(String file){
033         Properties property = new Properties();
034         if (StringKit.isBlank(file))
035             throw new IllegalArgumentException("Parameter of file can not be blank");
036         if (file.contains(".."))
037             throw new IllegalArgumentException("Parameter of file can not contains \"..\"");
038  
039         InputStream inputStream = null;
040         String fullFile;   // String fullFile = PathUtil.getWebRootPath() + file;
041         if (file.startsWith(File.separator))
042             fullFile = PathKit.getWebRootPath() + File.separator + "WEB-INF" + file;
043         else
044             fullFile = PathKit.getWebRootPath() + File.separator + "WEB-INF" + File.separator + file;
045  
046         try {
047             inputStream = new FileInputStream(new File(fullFile));
048             property.load(inputStream);
049         catch (Exception eOne) {
050             try {
051                 ClassLoader loader = Thread.currentThread().getContextClassLoader();
052                 property.load(loader.getResourceAsStream(file));
053             catch (IOException eTwo) {
054                 throw new IllegalArgumentException("Properties file loading failed: " + eTwo.getMessage());
055             }
056         }
057         finally {
058             try {if (inputStream != null) inputStream.close();} catch (IOException e) {e.printStackTrace();}
059         }
060         if (property != null){
061             for(Entry<Object,Object>  entry : property.entrySet()){
062                 this.properties.put(entry.getKey().toString(), entry.getValue());
063             }
064         }
065     }
066  
067     public String getProperty(String key) {
068         if(this.properties.containsKey(key)){
069             return properties.get(key).toString();
070         }
071         return null;
072     }
073  
074     public String getProperty(String key, String defaultValue) {
075         if(this.properties.containsKey(key)){
076             return properties.get(key).toString();
077         }
078         return defaultValue;
079     }
080  
081     public Integer getPropertyToInt(String key) {
082         Integer resultInt = null;
083         String resultStr = this.getProperty(key);
084         if (resultStr != null)
085             resultInt =  Integer.parseInt(resultStr);
086         return resultInt;
087     }
088  
089     public Integer getPropertyToInt(String key, Integer defaultValue) {
090         Integer result = getPropertyToInt(key);
091         return result != null ? result : defaultValue;
092     }
093  
094     public Boolean getPropertyToBoolean(String key) {
095         String resultStr = this.getProperty(key);
096         Boolean resultBool = null;
097         if (resultStr != null) {
098             if (resultStr.trim().equalsIgnoreCase("true"))
099                 resultBool = true;
100             else if (resultStr.trim().equalsIgnoreCase("false"))
101                 resultBool = false;
102         }
103         return resultBool;
104     }
105  
106     public Boolean getPropertyToBoolean(String key, boolean defaultValue) {
107         Boolean result = getPropertyToBoolean(key);
108         return result != null ? result : defaultValue;
109     }
110 }

使用例子:


1 public void configConstant(Constants me) {
2         //加载数据库配置文件
3         PropertyConfig.me().loadPropertyFile("database.properties");
4         PropertyConfig.me().loadPropertyFile("app.properties");
5                //设定为开发者模式
6  me.setDevMode(PropertyConfig.me().getPropertyToBoolean("jfinal.devmode"false));

1 public void configPlugin(Plugins me) {
2         //从配置文件中获取数据库配置项
3         PropertyConfig config = PropertyConfig.me();
4         String driver = config.getProperty("dataSource.driverClass");
5         String jdbcUrl = config.getProperty("dataSource.url");
6         String username = config.getProperty("dataSource.userName");
7         String password = config.getProperty("dataSource.password");

打完收工。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值