SpringBoot-08:SpringBoot采用json的方式实现前后台通用的配置文件
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
一。需求:
本篇博客是最近笔者做的一个项目,已经上线但是还在不断开发,有些页面上的配置,测试服务器和正式服务器的参数不同,需要经常改动,所以直接改页面肯定不合适!
so;产品经理提出一个需求,需要只建一个配置文件,存储正式服务器和测试服务器的配置,要求前后台通用,读取方便,修改方便
二。结构:
采用.json文件实现的前后台通用读取
三。知识点:
前后台不同的.json静态文件读取,解析方式
编写能让springboot识别到自定义静态资源的路径的插件
四。优点:
比.js,.properties文件,以及多次请求后台的静态.class文件要优良一些,无论从性能还是被浏览器缓存Gank的因素来讲,这个.json的方式确实优良
五。方案思路以及最后解决:
1.项目结构

2.创建.json文件---->TheServerURL.json
注意:里面的格式为json

{
"appId": "",
"secret": "",
"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
转载请注明出处!
如果有错误或疑惑,请您指出,互相探讨互相学习,感激不尽!!!!!!!!!!!!
SpringBoot-08:SpringBoot采用json的方式实现前后台通用的配置文件的更多相关文章
- DELPHI XE2 采用 JSON 的方式来序列化对象
DELPHI XE2 采用 JSON 的方式来序列化对象 以下代码测试通过.问题是里面的中文,在反序列化后是乱码. 1. 序列化对象为字符串,Subject 里面的中文看起来正常,仍然是中文: 2. ...
- springboot使用fastJson作为json解析框架
springboot使用fastJson作为json解析框架 springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用 〇.搭建spri ...
- 【转】SpringBoot启动服务的三种方式
1.IDEA启动 2.命令行启动 首先将命令行位置跳转到当前项目的根目录下,再输入“mvn spring-boot:run”命令,初次操作maven需要下载插件等待几分钟 3.命令行编译为jar启动 ...
- SpringBoot三种配置Dubbo的方式
*必须首先导入dubbo-starter (1).使用SpringBoot配置文件(application.properties或application.yml) dubbo.application. ...
- SpringBoot整合Servlet的两种方式
SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...
- springboot成神之——mybatis在spring-boot中使用的几种方式
本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...
- SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)
前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...
- SpringBoot系列-整合Mybatis(注解方式)
目录 一.常用注解说明 二.实战 三.测试 四.注意事项 上一篇文章<SpringBoot系列-整合Mybatis(XML配置方式)>介绍了XML配置方式整合的过程,本文介绍下Spring ...
- SpringBoot集成MyBatis的Bean配置方式
SpringBoot集成MyBatis的Bean配置方式 SpringBoot是一款轻量级开发的框架,简化了很多原先的xml文件配置方式,接下来就介绍一下如何不适用XML来配置Mybatis spri ...
随机推荐
- UVa 1640 - The Counting Problem(数论)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- BZOJ4321:queue2(DP)
Description n 个沙茶,被编号 1~n.排完队之后,每个沙茶希望,自己的相邻的两人只要无一个人的编号和自己的编号相差为 1(+1 或-1)就行.现在想知道,存在多少方案满足沙茶们如此不苛刻 ...
- F2eTest和uirecorder自动化测试环境部署填坑记录
坑1:尝试部署的时候只在opennode.bat里面填写了两个浏览器,测试通过后再增加其他浏览器,页面上一直不显示. 填坑:需要清空数据库里的`wd_browsers`和`wd_nodes`表,然后重 ...
- MAC下常用命令的中文帮助文档(man) 出现错误
MacdeMacBook-Pro:Desktop mac$ tar -xf manpages-zh-1.5.2.tar.bz2 MacdeMacBook-Pro:~ root# cd /Users/m ...
- 递归根据父ID 找所有子类ID
function getinfo($pid){ $str = ''; $row = M('user')->where(array('pid'=>$pid))->select(); i ...
- LD-sketch源码阅读
目录 util.h hash.hpp/cpp mangle函数 GenHashSeed函数 AwareHash模块 LDSketch.hpp/cpp LDSketch更新函数,对一个sketch插入键 ...
- 阿里云阿里免费ssl wap网站在手机微信、手机浏览器无法访问
图片可以访问,样式无法显示 https://css.cnbuses.com/css/wap/gonggong.css 1,怀疑是开了代理访问. 关闭后还是访问空白. 2.在手机浏览器打开,提示该网站的 ...
- mysql修改数据表自增步长
可以修改系统变量 auto_increment_increment mysql> SHOW VARIABLES LIKE 'auto_inc%'; +---------------------- ...
- execute immediate
首先在这里发发牢骚,指责下那些刻板的书写方式,不考虑读者理不理解,感觉就是给专业人员用来复习用的一样,没有前戏,直接就高潮,实在受不了!没基础或基础差的完全不知道发生了什么,一脸懵逼的看着,一星差评! ...
- IDEA导入eclipse项目并部署到tomcat
1.首先引入本地项目 我这里是maven项目就直接选择的以maven项目引入,如果选eclipse的话,pom文件不会被初始化,部署tomcat会出问题 这项选完后,就一路next,jdk可以在引入的 ...