1. 程式人生 > >SpringBoot-08:SpringBoot採用json的方式實現前後臺通用的配置檔案

SpringBoot-08:SpringBoot採用json的方式實現前後臺通用的配置檔案

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(); } } }