java项目中的全局变量
来源:互联网 发布:二环十三郎 知乎 编辑:程序博客网 时间:2024/05/22 04:52
1,全局配置类
“JAVA中不应该有所谓全局变量的概念,全局变量严重影响了封装和模块化,所以如果你的程序中需要所谓的全局变量,那一定是你对程序的设计出了问题。”
这句话我也不太理解,但是觉得项目里还是需要有所谓的全局配置,全局配置写在配置文件中,然后加载到项目里
/** * Copyright © 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 settingjdbc.type=mysqljdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://192.168.2.114:3306/jeeplus_schema?useUnicode=true&characterEncoding=utf-8jdbc.username=rootjdbc.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 settingsjdbc.pool.init=1jdbc.pool.minIdle=3jdbc.pool.maxActive=20#jdbc.testSql=SELECT 'x'jdbc.testSql=SELECT 'x' FROM DUAL#redis settingsredis.keyPrefix=jeeplusredis.host=127.0.0.1redis.port=6379#============================##===== System settings ======##============================##\u4ea7\u54c1\u4fe1\u606f\u8bbe\u7f6eproductName=JeePlus \u5feb\u901f\u5f00\u53d1\u5e73\u53f0copyrightYear=2014version=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/categorydemoMode=false#\u7ba1\u7406\u57fa\u7840\u8def\u5f84, \u9700\u540c\u6b65\u4fee\u6539\uff1aweb.xmladminPath=/a#\u524d\u7aef\u57fa\u7840\u8def\u5f84frontPath=/f#\u7f51\u7ad9URL\u540e\u7f00urlSuffix=.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\u5f55notAllowRefreshIndex=false#\u662f\u5426\u5141\u8bb8\u591a\u8d26\u53f7\u540c\u65f6\u767b\u5f55user.multiAccountLogin=true#\u5206\u9875\u914d\u7f6epage.pageSize=10#\u7855\u6b63\u7ec4\u4ef6\u662f\u5426\u4f7f\u7528\u7f13\u5b58supcan.useCache=false#\u901a\u77e5\u95f4\u9694\u65f6\u95f4\u8bbe\u7f6e, \u5355\u4f4d\uff1a\u6beb\u79d2, 30s=30000ms, 60s=60000msoa.notify.remind.interval=60000#============================##==== Framework settings ====##============================##\u4f1a\u8bdd\u8d85\u65f6\uff0c \u5355\u4f4d\uff1a\u6beb\u79d2\uff0c 20m=1200000ms, 30m=1800000ms, 60m=3600000mssession.sessionTimeout=1800000#\u4f1a\u8bdd\u6e05\u7406\u95f4\u9694\u65f6\u95f4\uff0c \u5355\u4f4d\uff1a\u6beb\u79d2\uff0c2m=120000ms\u3002session.sessionTimeoutClean=120000#\u7f13\u5b58\u8bbe\u7f6eehcache.configFile=cache/ehcache-local.xml#ehcache.configFile=cache/ehcache-rmi.xml#\u7d22\u5f15\u9875\u8def\u5f84web.view.index=/a#\u89c6\u56fe\u6587\u4ef6\u5b58\u653e\u8def\u5f84web.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.xmlweb.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\u7f00web.staticFile=.css,.js,.png,.jpg,.gif,.jpeg,.bmp,.ico,.swf,.psd,.htc,.htm,.html,.crx,.xpi,.exe,.ipa,.apk#\u5355\u70b9\u767b\u5f55CAS\u8bbe\u7f6ecas.server.url=http://127.0.0.1:8180/cascas.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=100jvm=100ram=100
2,spring自带注入方法
配置文件内容
spring使用方法
spring加载配置文件
阅读全文
0 0
- java项目中的全局变量
- java中的全局变量
- java中的全局变量
- java中的全局变量
- java中的全局变量
- java中的全局变量与局部变量
- Java中的全局变量和局部变量
- java中的全局变量与局部变量
- java中的局部变量和全局变量
- Java中的变量:全局变量与局部变量
- Java全局变量
- web项目设置全局变量
- Java项目中的分包
- 项目中的java集锦
- java中的项目!
- Java项目中的分包
- Java项目中的classpath
- java项目中的路径
- MyEclipse 反编译插件的安装
- RAML 1.0中引用JSON Schema的方法详解
- linux 在线安装 jdk tomcat mysql eclipse
- 怎样选择免备案虚拟主机空间
- 四、获取项目
- java项目中的全局变量
- Matlab图像处理转灰度图,绘制直方图,直方图均衡化
- iOS如何指定某个页面可以旋转屏幕,其余控制器都正常竖屏
- Android之引用包含远程依赖库的aar(引用aar时找不到arr的远程依赖库问题)
- plsql是可以写多个select的,但是一定要加上;号,不然就会报错
- curl_easy_setopt参数详解(转)
- HDU-2069 Coin Change (母函数)
- 查询链接字符串函数
- java并发编程2