SpringBoot-08:SpringBoot採用json的方式實現前後臺通用的配置檔案
摘要:
------------吾亦無他,唯手熟爾,謙卑若愚,好學若飢-------------
一。 需求 :
本篇部落格是最近筆者做的一個專案,已經上線但是還在不斷開發,有些頁面上的配置,測試伺服器和正式伺服器的引數不同,需要經常改動,所以直接改頁面肯定不合適!
so; ...
------------吾亦無他,唯手熟爾,謙卑若愚,好學若飢-------------
一。 需求 :
本篇部落格是最近筆者做的一個專案,已經上線但是還在不斷開發,有些頁面上的配置,測試伺服器和正式伺服器的引數不同,需要經常改動,所以直接改頁面肯定不合適!
so; 產品經理提出一個需求,需要 只建一個配置檔案 , 儲存正式伺服器和測試伺服器的配置 , 要求前後臺通用 , 讀取方便 , 修改方便
二。 結構 :
採用 .json檔案實現 的 前後臺通用讀取
三。 知識點 :
前後臺不同的.json靜態檔案讀取,解析方式
編寫能 讓springboot識別到自定義靜態資源的路徑 的 外掛
四。 優點 :
比.js , .properties 檔案,以及 多次請求後臺的靜態.class 檔案要 優良 一些,無論從 效能 還是被 瀏覽器快取Gank 的因素來講,這個 .json的方式確實優良
五。 方案思路以及最後解決 :
1. 專案結構
2. 建立.json檔案 ----> TheServerURL.json
注意:裡面的 格式為json
{ "appId": "123456", "secret": "123456789123", "url61": "http://192.168.1.1", "urlhead": "baidu.com", "urlinit": "www.hao123.com", "urlman": "mi.com", "justChangeThisURL":"https://www.cnblogs.com/DawnCHENXI/" } 範本
3. 給springboot註冊外掛ApplicationConfig ,讓他識別到放置於resource下的靜態資源(.json放在此處是因為修改方便,打成war包之後,它很好找)
package com.xy.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; //這個註解需要啟動類去設定一下,否者不會識別 @Configuration public class ApplicationConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { /** * 如果我們將/xxxx/** 修改為 /** 與預設的相同時,則會覆蓋系統的配置,可以多次使用 addResourceLocations 新增目錄, * 優先順序先新增的高於後新增的。 * * 如果是/xxxx/** 引用靜態資源 加不加/xxxx/ 均可,因為系統預設配置(/**)也會作用 * 如果是/** 會覆蓋預設配置,應用addResourceLocations新增所有會用到的靜態資源地址,系統預設不會再起作用 */ registry.addResourceHandler("/**") .addResourceLocations("classpath:/META-INF/resources/") .addResourceLocations("classpath:/resources/") .addResourceLocations("classpath:/static/") .addResourceLocations("classpath:/public/") .addResourceLocations("classpath:/"); registry.addResourceHandler("classpath:/mybatis/**"); super.addResourceHandlers(registry); } }
4. 前臺頁面的使用方式 (需要jquery支援)
function test() { var dataroot="/TheServerURL.json";//json檔案路徑 var urlTemp; var appid; $.getJSON(dataroot, function(data) { urlTemp = "http://"+data.justChangeThisURL+"/teacher100/skip/login" appid=data.appId var path = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri=" + urlTemp + "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; window.location.href = path; }); } 範本
5. 後端構建個工具類,用來承載前端傳回來的值 (TheServerURL )
package com.xy.model; public class TheServerURL { //測試伺服器 public static String urlhead=""; public static String urlinit=""; public static String urlman=""; public static String appId=""; public static String secret=""; public static String url61 = ""; public staticString justChangeThisURL=""; public static String getJustChangeThisURL() { return justChangeThisURL; } public static void setJustChangeThisURL(String justChangeThisURL) { TheServerURL.justChangeThisURL = justChangeThisURL; } public static String getUrlhead() { return urlhead; } public static void setUrlhead(String urlhead) { TheServerURL.urlhead = urlhead; } public static String getUrlinit() { return urlinit; } public static void setUrlinit(String urlinit) { TheServerURL.urlinit = urlinit; } public static String getUrlman() { return urlman; } public static void setUrlman(String urlman) { TheServerURL.urlman = urlman; } public static String getAppId() { return appId; } public static void setAppId(String appId) { TheServerURL.appId = appId; } public static String getSecret() { return secret; } public static void setSecret(String secret) { TheServerURL.secret = secret; } public static String getUrl61() { return url61; } public static void setUrl61(String url61) { TheServerURL.url61 = url61; } }
6. 註冊外掛,在啟動專案的時候讀取配置 (這個可以之後再去拓展,或者再寫介面,實時重新整理)
package com.xy.config; import com.alibaba.fastjson.JSON; import com.xy.model.TheServerURL; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import java.io.*; import java.util.HashMap; @Configuration public class TheURLLoadBean { public TheURLLoadBean(){ try { //讀取到靜態資原始檔 org.springframework.core.io.Resource resource = new ClassPathResource("TheServerURL.json"); File file = null; file = resource.getFile(); //使用io讀出資料 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String str = null; StringBuilder all=new StringBuilder(); while((str = br.readLine()) != null){ all.append(str); } if(all!=null){ //採用阿里的fastjson解析這個json HashMap hashMap=JSON.parseObject(all.toString(),HashMap.class); //裝配給這個工具類的靜態欄位 TheServerURL.urlhead=(String) hashMap.get("urlhead"); TheServerURL.urlinit=(String) hashMap.get("urlinit"); TheServerURL.urlman=(String) hashMap.get("urlman"); TheServerURL.appId=(String) hashMap.get("appId"); TheServerURL.secret=(String) hashMap.get("secret"); TheServerURL.url61=(String) hashMap.get("url61"); TheServerURL.justChangeThisURL=(String) hashMap.get("justChangeThisURL"); } } catch (IOException e) { e.printStackTrace(); } } }
7. 後端使用配置檔案的引數 ( 直接用這個 工具類類名 . 靜態欄位名 )就可以呼叫到
TheServerURL.appId