------------吾亦無他,唯手熟爾,謙卑若愚,好學若飢-------------
一。需求:
本篇博客是最近筆者做的一個項目,已經上線但是還在不斷開發,有些頁面上的配置,測試服務器和正式服務器的參數不同,需要經常改動,所以直接改頁面肯定不合適!
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 static String 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
筆者:晨曦Dawn
轉載請注明出處!
如果有錯誤或疑惑,請您指出,互相探討互相學習,感激不盡!!!!!!!!!!!!