1. 程式人生 > >java專案中的全域性變數

java專案中的全域性變數

1,全域性配置類

“JAVA中不應該有所謂全域性變數的概念,全域性變數嚴重影響了封裝和模組化,所以如果你的程式中需要所謂的全域性變數,那一定是你對程式的設計出了問題。”
這句話我也不太理解,但是覺得專案裡還是需要有所謂的全域性配置,全域性配置寫在配置檔案中,然後載入到專案裡

/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.common.config;

import java.io.File;
import
java.io.FileOutputStream; import java.io.IOException; import java.io.Reader; import java.util.Map; import java.util.Properties; import org.apache.ibatis.io.Resources; import org.springframework.core.io.DefaultResourceLoader; import com.ckfinder.connector.ServletContextFactory; import com.google.common.collect.Maps; import
com.jeeplus.common.utils.PropertiesLoader; import com.jeeplus.common.utils.StringUtils; /** * 全域性配置類 * @author jeeplus * @version 2014-06-25 */ public class Global { /** * 當前物件例項 */ private static Global global = new Global(); /* * */ public static String APP_CACHE="APPCACHE"
; public static String APP_CACHE_checkcode="APPCACHEcheckcode"; //public static String APP_CACHE_phone="APPCACHEphone"; public static String APP_TOKEN="APPTOKEN"; public static String APP_SHORT_CACHE="APPSHORTCACHE"; /** * 儲存全域性屬性值 */ private static Map<String, String> map = Maps.newHashMap(); /** * 屬性檔案載入物件 */ private static PropertiesLoader loader = new PropertiesLoader("jeeplus.properties"); /** * 顯示/隱藏 */ public static final String SHOW = "1"; public static final String HIDE = "0"; /** * 是/否 */ public static final String YES = "1"; public static final String NO = "0"; /** * 對/錯 */ public static final String TRUE = "true"; public static final String FALSE = "false"; /** * 上傳檔案基礎虛擬路徑 */ public static final String USERFILES_BASE_URL = "/userfiles/"; /** * 獲取當前物件例項 */ public static Global getInstance() { return global; } /** * 獲取配置 * @see ${fns:getConfig('adminPath')} */ public static String getConfig(String key) { String value = map.get(key); if (value == null){ value = loader.getProperty(key); map.put(key, value != null ? value : StringUtils.EMPTY); } return value; } /** * 獲取管理端根路徑 */ 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); } /** * 在修改系統使用者和角色時是否同步到Activiti */ public static Boolean isSynActivitiIndetity() { String dm = getConfig("activiti.isSynActivitiIndetity"); return "true".equals(dm) || "1".equals(dm); } /** * 頁面獲取常量 * @see ${fns:getConst('YES')} */ public static Object getConst(String field) { try { return Global.class.getField(field).get(null); } catch (Exception e) { // 異常代表無配置,這裡什麼也不做 } return null; } /** * 獲取上傳檔案的根目錄 * @return */ public static String getUserfilesBaseDir() { String dir = getConfig("userfiles.basedir"); if (StringUtils.isBlank(dir)){ try { dir = ServletContextFactory.getServletContext().getRealPath("/"); } catch (Exception e) { return ""; } } if(!dir.endsWith("/")) { dir += "/"; } // System.out.println("userfiles.basedir: " + dir); return dir; } /** * 獲取工程路徑 * @return */ public static String getProjectPath(){ // 如果配置了工程路徑,則直接返回,否則自動獲取。 String projectPath = Global.getConfig("projectPath"); if (StringUtils.isNotBlank(projectPath)){ return projectPath; } try { File file = new DefaultResourceLoader().getResource("").getFile(); if (file != null){ while(true){ File f = new File(file.getPath() + File.separator + "src" + File.separator + "main"); if (f == null || f.exists()){ break; } if (file.getParentFile() != null){ file = file.getParentFile(); }else{ break; } } projectPath = file.toString(); } } catch (IOException e) { e.printStackTrace(); } return projectPath; } /** * 寫入properties資訊 * * @param key * 名稱 * @param value * 值 */ public static void modifyConfig(String key, String value) { try { // 從輸入流中讀取屬性列表(鍵和元素對) Properties prop = getProperties(); prop.setProperty(key, value); String path = Global.class.getResource("/jeeplus.properties").getPath(); FileOutputStream outputFile = new FileOutputStream(path); prop.store(outputFile, "modify"); outputFile.close(); outputFile.flush(); } catch (Exception e) { e.printStackTrace(); } } /** * 返回 Properties * @param fileName 檔名 (注意:載入的是src下的檔案,如果在某個包下.請把包名加上) * @param * @return */ public static Properties getProperties(){ Properties prop = new Properties(); try { Reader reader = Resources.getResourceAsReader("/jeeplus.properties"); prop.load(reader); } catch (Exception e) { return null; } return prop; } }

全域性配置檔案

jeeplus.properties配置檔案

#============================#
#===== Database sttings =====#
#============================#

#oracle database settings
#jdbc.type=oracle
#jdbc.driver=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:jeeplus
#jdbc.username=liugf
#jdbc.password=fnst1234

#mysql database setting
jdbc.type=mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.2.114:3306/jeeplus_schema?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

#mssql database settings
#jdbc.type=mssql
#jdbc.driver=net.sourceforge.jtds.jdbc.Driver
#jdbc.url=jdbc:jtds:sqlserver://localhost:1433/jeeplus
#jdbc.username=sa
#jdbc.password=sa

#pool settings
jdbc.pool.init=1
jdbc.pool.minIdle=3
jdbc.pool.maxActive=20

#jdbc.testSql=SELECT 'x'
jdbc.testSql=SELECT 'x' FROM DUAL

#redis settings
redis.keyPrefix=jeeplus
redis.host=127.0.0.1
redis.port=6379

#============================#
#===== System settings ======#
#============================#

#\u4ea7\u54c1\u4fe1\u606f\u8bbe\u7f6e
productName=JeePlus \u5feb\u901f\u5f00\u53d1\u5e73\u53f0
copyrightYear=2014
version=V1.2.6

#\u6f14\u793a\u6a21\u5f0f: \u4e0d\u80fd\u64cd\u4f5c\u548c\u4fdd\u5b58\u7684\u6a21\u5757\uff1a sys: area/office/user/role/menu/dict, cms: site/category
demoMode=false

#\u7ba1\u7406\u57fa\u7840\u8def\u5f84, \u9700\u540c\u6b65\u4fee\u6539\uff1aweb.xml
adminPath=/a

#\u524d\u7aef\u57fa\u7840\u8def\u5f84
frontPath=/f

#\u7f51\u7ad9URL\u540e\u7f00
urlSuffix=.html

#\u662f\u5426\u4e0d\u5141\u8bb8\u5237\u65b0\u4e3b\u9875\uff0c\u4e0d\u5141\u8bb8\u60c5\u51b5\u4e0b\uff0c\u5237\u65b0\u4e3b\u9875\u4f1a\u5bfc\u81f4\u91cd\u65b0\u767b\u5f55
notAllowRefreshIndex=false

#\u662f\u5426\u5141\u8bb8\u591a\u8d26\u53f7\u540c\u65f6\u767b\u5f55
user.multiAccountLogin=true

#\u5206\u9875\u914d\u7f6e
page.pageSize=10

#\u7855\u6b63\u7ec4\u4ef6\u662f\u5426\u4f7f\u7528\u7f13\u5b58
supcan.useCache=false

#\u901a\u77e5\u95f4\u9694\u65f6\u95f4\u8bbe\u7f6e, \u5355\u4f4d\uff1a\u6beb\u79d2, 30s=30000ms, 60s=60000ms
oa.notify.remind.interval=60000

#============================#
#==== Framework settings ====#
#============================#

#\u4f1a\u8bdd\u8d85\u65f6\uff0c \u5355\u4f4d\uff1a\u6beb\u79d2\uff0c 20m=1200000ms, 30m=1800000ms, 60m=3600000ms
session.sessionTimeout=1800000
#\u4f1a\u8bdd\u6e05\u7406\u95f4\u9694\u65f6\u95f4\uff0c \u5355\u4f4d\uff1a\u6beb\u79d2\uff0c2m=120000ms\u3002
session.sessionTimeoutClean=120000

#\u7f13\u5b58\u8bbe\u7f6e
ehcache.configFile=cache/ehcache-local.xml
#ehcache.configFile=cache/ehcache-rmi.xml

#\u7d22\u5f15\u9875\u8def\u5f84
web.view.index=/a

#\u89c6\u56fe\u6587\u4ef6\u5b58\u653e\u8def\u5f84
web.view.prefix=/webpage/
web.view.suffix=.jsp

#\u6700\u5927\u6587\u4ef6\u4e0a\u4f20\u9650\u5236\uff0c\u5355\u4f4d\u5b57\u8282. 10M=10*1024*1024(B)=10485760 bytes\uff0c\u9700\u540c\u6b65\u4fee\u6539\uff1ackfinder.xml
web.maxUploadSize=10485760

#\u65e5\u5fd7\u62e6\u622a\u8bbe\u7f6e\uff0c\u6392\u9664\u7684URI\uff1b\u5305\u542b @RequestMapping\u6ce8\u89e3\u7684value\u3002\uff08\u5df2\u4f5c\u5e9f\uff09
#web.logInterceptExcludeUri=/, /login, /sys/menu/tree, /sys/menu/treeData, /oa/oaNotify/self/count
#web.logInterceptIncludeRequestMapping=save, delete, import, updateSort

#\u9759\u6001\u6587\u4ef6\u540e\u7f00
web.staticFile=.css,.js,.png,.jpg,.gif,.jpeg,.bmp,.ico,.swf,.psd,.htc,.htm,.html,.crx,.xpi,.exe,.ipa,.apk

#\u5355\u70b9\u767b\u5f55CAS\u8bbe\u7f6e
cas.server.url=http://127.0.0.1:8180/cas
cas.project.url=http://127.0.0.1:8080/jeeplus


#\u4e0a\u4f20\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84, \u8def\u5f84\u4e2d\u4e0d\u5141\u8bb8\u5305\u542b\u201cuserfiles\u201d
#userfiles.basedir=D:/jeeplus

#\u5de5\u7a0b\u8def\u5f84\uff0c\u5728\u4ee3\u7801\u751f\u6210\u65f6\u83b7\u53d6\u4e0d\u5230\u5de5\u7a0b\u8def\u5f84\u65f6\uff0c\u53ef\u518d\u6b64\u6307\u5b9a\u7edd\u5bf9\u8def\u5f84\u3002
#projectPath=D\:\\workspace\\jeeplus

#
fromEmail=
toEmail=
emailName=
emailPassword=
cpu=100
jvm=100
ram=100

2,spring自帶注入方法

  1. 配置檔案內容
    這裡寫圖片描述

  2. spring使用方法
    這裡寫圖片描述

  3. spring載入配置檔案
    這裡寫圖片描述