• java jeesite.properties配置文件属性提取


      1 package com.thinkgem.jeesite.common.config;
      2 
      3 import java.io.UnsupportedEncodingException;
      4 import java.util.Map;
      5 
      6 import org.apache.commons.lang3.StringUtils;
      7 import org.springframework.util.Assert;
      8 
      9 import com.google.common.collect.Maps;
     10 import com.thinkgem.jeesite.common.utils.PropertiesLoader;
     11 
     12 /**
     13  * 全局配置类
     14  * @author ThinkGem
     15  * @version 2013-03-23
     16  */
     17 public class Global {
     18     
     19     /**
     20      * 保存全局属性值
     21      */
     22     private static Map<String, String> map = Maps.newHashMap();
     23     
     24     /**
     25      * 属性文件加载对象
     26      */
     27     private static PropertiesLoader propertiesLoader = new PropertiesLoader("jeesite.properties");
     28 
     29     /**
     30      * 获取配置
     31      */
     32     public static String getConfig(String key) {
     33         return getConfig(key, null);
     34     }
     35     /**
     36      * 获取配置
     37      */
     38     public static String getConfig(String key, String defaultVal) {
     39         String value = map.get(key);
     40         if (value == null){
     41             value = propertiesLoader.getProperty(key);
     42             map.put(key, value);
     43         }
     44         if(StringUtils.isBlank(value)
     45                 && StringUtils.isNotBlank(defaultVal) ){
     46             return defaultVal;
     47         }
     48         return value;
     49     }
     50 
     51     /////////////////////////////////////////////////////////
     52     
     53 
     54     public static String getDataDownDbUser() {
     55         return Global.getConfig("dataDownDbUser","NX_DOWN_DATA") ;
     56     }
     57     
     58     /**
     59      * 获取管理端根路径
     60      */
     61     public static String getAdminPath() {
     62         return getConfig("adminPath");
     63     }
     64     
     65     /**
     66      * 获取前端根路径
     67      */
     68     public static String getFrontPath() {
     69         return getConfig("frontPath");
     70     }
     71     
     72     /**
     73      * 获取URL后缀
     74      */
     75     public static String getUrlSuffix() {
     76         return getConfig("urlSuffix");
     77     }
     78     
     79     /**
     80      * 是否是演示模式,演示模式下不能修改用户、角色、密码、菜单、授权
     81      */
     82     public static Boolean isDemoMode() {
     83         String dm = getConfig("demoMode");
     84         return "true".equals(dm) || "1".equals(dm);
     85     }
     86 
     87     /**
     88      * 获取CKFinder上传文件的根目录
     89      * @return
     90      */
     91     public static String getCkBaseDir() {
     92         String dir = getConfig("userfiles.basedir");
     93         Assert.hasText(dir, "配置文件里没有配置userfiles.basedir属性");
     94         if(!dir.endsWith("/")) {
     95             dir += "/";
     96         }
     97         return dir;
     98     }
     99     
    100 }

    简单的打包:

    package com.thinkgem.jeesite.common.config;
    
    import java.io.UnsupportedEncodingException;
    import java.util.Map;
    
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.util.Assert;
    
    import com.google.common.collect.Maps;
    import com.thinkgem.jeesite.common.utils.PropertiesLoader;
    
    
    public class Global {
        
        /**
         * 保存全局属性值
         */
        private static Map<String, String> map = Maps.newHashMap();
        
        /**
         * 属性文件加载对象
         */
        private static PropertiesLoader propertiesLoader = new PropertiesLoader("jeesite.properties");
    
        /**
         * 获取配置
         */
        public static String getConfig(String key) {
            return getConfig(key, null);
        }
        /**
         * 获取配置
         */
        public static String getConfig(String key, String defaultVal) {
            String value = map.get(key);
            if (value == null){
                value = propertiesLoader.getProperty(key);
                map.put(key, value);
            }
            if(StringUtils.isBlank(value)
                    && StringUtils.isNotBlank(defaultVal) ){
                return defaultVal;
            }
            return value;
        }
    
        /////////////////////////////////////////////////////////
        
    
        public static String getDataDownDbUser() {
            return Global.getConfig("dataDownDbUser","NX_DOWN_DATA") ;
        }
        
        /**
         * 获取管理端根路径
         */
        public static String getAdminPath() {
            return getConfig("adminPath");
        }
        
        /**
         * 获取前端根路径
         */
        public static String getFrontPath() {
            return getConfig("frontPath");
        }
        
        /**
         * 获取URL后缀
         */
        public static String getUrlSuffix() {
            return getConfig("urlSuffix");
        }
        
        /**
         * 是否是演示模式,演示模式下不能修改用户、角色、密码、菜单、授权
         */
        public static Boolean isDemoMode() {
            String dm = getConfig("demoMode");
            return "true".equals(dm) || "1".equals(dm);
        }
    
        /**
         * 获取CKFinder上传文件的根目录
         * @return
         */
        public static String getCkBaseDir() {
            String dir = getConfig("userfiles.basedir");
            Assert.hasText(dir, "配置文件里没有配置userfiles.basedir属性");
            if(!dir.endsWith("/")) {
                dir += "/";
            }
            return dir;
        }
        
    }

    调用示例:

    Global.getDataDownDbUser();
  • 相关阅读:
    C# 全局变量
    [C#]续:利用键代码自动转换生成字母键或其它键信息
    [WPF](小结2)DataGrid嵌套DataGrid(也叫主从表)
    [C#]winform窗口托盘
    C# arrayList动态添加对象元素,并取出对象元素的方法
    [WPF](小结3)DataGridInTreeView树嵌表
    [WPF](小结4)TreeView的数据分层模板
    [WPF](小结1)ListBox嵌套ListBox
    [C#]利用键代码自动转换生成字母键或其它键信息
    [C#]使用API 获取设置系统热键和快捷键
  • 原文地址:https://www.cnblogs.com/tangzeqi/p/8743093.html
Copyright © 2020-2023  润新知