日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

SpringBoot四種讀取properties文件的方式(小結(jié))

瀏覽:26日期:2023-05-15 16:59:31

前言

在項(xiàng)目開發(fā)中經(jīng)常會(huì)用到配置文件,配置文件的存在解決了很大一份重復(fù)的工作。今天就分享四種在Springboot中獲取配置文件的方式。

注:前三種測(cè)試配置文件為springboot默認(rèn)的application.properties文件

#######################方式一#########################com.zyd.type3=Springboot - @ConfigurationPropertiescom.zyd.title3=使用@ConfigurationProperties獲取配置文件#mapcom.zyd.login[username]=zhangdeshuaicom.zyd.login[password]=zhenshuaicom.zyd.login[callback]=http://www.flyat.cc#listcom.zyd.urls[0]=http://ztool.cccom.zyd.urls[1]=http://ztool.cc/format/jscom.zyd.urls[2]=http://ztool.cc/str2imagecom.zyd.urls[3]=http://ztool.cc/json2Entitycom.zyd.urls[4]=http://ztool.cc/ua#######################方式二#########################com.zyd.type=Springboot - @Valuecom.zyd.title=使用@Value獲取配置文件#######################方式三#########################com.zyd.type2=Springboot - Environmentcom.zyd.title2=使用Environment獲取配置文件

一、@ConfigurationProperties方式

自定義配置類:PropertiesConfig.java

package com.zyd.property.config;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.boot.context.properties.ConfigurationProperties;//import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;/** * 對(duì)應(yīng)上方配置文件中的第一段配置 * @author <a href='mailto:yadong.zhang0415@gmail.com' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' >yadong.zhang</a> * @date 2017年6月1日 下午4:34:18 * @version V1.0 * @since JDK : 1.7 */@Component@ConfigurationProperties(prefix = 'com.zyd')// PropertySource默認(rèn)取application.properties// @PropertySource(value = 'config.properties')public class PropertiesConfig { public String type3; public String title3; public Map<String, String> login = new HashMap<String, String>(); public List<String> urls = new ArrayList<>(); public String getType3() { return type3; } public void setType3(String type3) { this.type3 = type3; } public String getTitle3() { try { return new String(title3.getBytes('ISO-8859-1'), 'UTF-8'); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return title3; } public void setTitle3(String title3) { this.title3 = title3; } public Map<String, String> getLogin() { return login; } public void setLogin(Map<String, String> login) { this.login = login; } public List<String> getUrls() { return urls; } public void setUrls(List<String> urls) { this.urls = urls; }}

程序啟動(dòng)類:Applaction.java

package com.zyd.property;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.zyd.property.config.PropertiesConfig;/** * @author <a href='mailto:yadong.zhang0415@gmail.com' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' >yadong.zhang</a> * @date 2017年6月1日 下午3:49:30 * @version V1.0 * @since JDK : 1.7 */@SpringBootApplication@RestControllerpublic class Applaction { @Autowired private PropertiesConfig propertiesConfig; /** * * 第一種方式:使用`@ConfigurationProperties`注解將配置文件屬性注入到配置對(duì)象類中 * * @author zyd * @throws UnsupportedEncodingException * @since JDK 1.7 */ @RequestMapping('/config') public Map<String, Object> configurationProperties() { Map<String, Object> map = new HashMap<String, Object>(); map.put('type', propertiesConfig.getType3()); map.put('title', propertiesConfig.getTitle3()); map.put('login', propertiesConfig.getLogin()); map.put('urls', propertiesConfig.getUrls()); return map; } public static void main(String[] args) throws Exception { SpringApplication application = new SpringApplication(Applaction.class); application.run(args); }}

訪問(wèn)結(jié)果:

{'title':'使用@ConfigurationProperties獲取配置文件','urls':['http://ztool.cc','http://ztool.cc/format/js','http://ztool.cc/str2image','http://ztool.cc/json2Entity','http://ztool.cc/ua'],'login':{'username':'zhangdeshuai','callback':'http://www.flyat.cc','password':'zhenshuai'},'type':'Springboot - @ConfigurationProperties'}

二、使用@Value注解方式

程序啟動(dòng)類:Applaction.java

import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author <a href='mailto:yadong.zhang0415@gmail.com' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' >yadong.zhang</a> * @date 2017年6月1日 下午3:49:30 * @version V1.0 * @since JDK : 1.7 */@SpringBootApplication@RestControllerpublic class Applaction { @Value('${com.zyd.type}') private String type; @Value('${com.zyd.title}') private String title; /** * * 第二種方式:使用`@Value('${propertyName}')`注解 * * @author zyd * @throws UnsupportedEncodingException * @since JDK 1.7 */ @RequestMapping('/value') public Map<String, Object> value() throws UnsupportedEncodingException { Map<String, Object> map = new HashMap<String, Object>(); map.put('type', type); // *.properties文件中的中文默認(rèn)以ISO-8859-1方式編碼,因此需要對(duì)中文內(nèi)容進(jìn)行重新編碼 map.put('title', new String(title.getBytes('ISO-8859-1'), 'UTF-8')); return map; } public static void main(String[] args) throws Exception { SpringApplication application = new SpringApplication(Applaction.class); application.run(args); }}

訪問(wèn)結(jié)果:

{'title':'使用@Value獲取配置文件','type':'Springboot - @Value'}

三、使用Environment

程序啟動(dòng)類:Applaction.java

package com.zyd.property;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.core.env.Environment;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author <a href='mailto:yadong.zhang0415@gmail.com' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' >yadong.zhang</a> * @date 2017年6月1日 下午3:49:30 * @version V1.0 * @since JDK : 1.7 */@SpringBootApplication@RestControllerpublic class Applaction { @Autowired private Environment env; /** * * 第三種方式:使用`Environment` * * @author zyd * @throws UnsupportedEncodingException * @since JDK 1.7 */ @RequestMapping('/env') public Map<String, Object> env() throws UnsupportedEncodingException { Map<String, Object> map = new HashMap<String, Object>(); map.put('type', env.getProperty('com.zyd.type2')); map.put('title', new String(env.getProperty('com.zyd.title2').getBytes('ISO-8859-1'), 'UTF-8')); return map; } public static void main(String[] args) throws Exception { SpringApplication application = new SpringApplication(Applaction.class); application.run(args); }}

訪問(wèn)結(jié)果:

{'title':'使用Environment獲取配置文件','type':'Springboot - Environment'}

四、使用PropertiesLoaderUtils

app-config.properties

#### 通過(guò)注冊(cè)監(jiān)聽器(`Listeners`) + `PropertiesLoaderUtils`的方式com.zyd.type=Springboot - Listenerscom.zyd.title=使用Listeners + PropertiesLoaderUtils獲取配置文件com.zyd.name=zydcom.zyd.address=Beijingcom.zyd.company=in

PropertiesListener.java 用來(lái)初始化加載配置文件

package com.zyd.property.listener;import org.springframework.boot.context.event.ApplicationStartedEvent;import org.springframework.context.ApplicationListener;import com.zyd.property.config.PropertiesListenerConfig;/** * 配置文件監(jiān)聽器,用來(lái)加載自定義配置文件 * * @author <a href='mailto:yadong.zhang0415@gmail.com' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' >yadong.zhang</a> * @date 2017年6月1日 下午3:38:25 * @version V1.0 * @since JDK : 1.7 */public class PropertiesListener implements ApplicationListener<ApplicationStartedEvent> { private String propertyFileName; public PropertiesListener(String propertyFileName) { this.propertyFileName = propertyFileName; } @Override public void onApplicationEvent(ApplicationStartedEvent event) { PropertiesListenerConfig.loadAllProperties(propertyFileName); }}

PropertiesListenerConfig.java 加載配置文件內(nèi)容

package com.zyd.property.config;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import java.util.Properties;import org.springframework.beans.BeansException;import org.springframework.core.io.support.PropertiesLoaderUtils;/** * 第四種方式:PropertiesLoaderUtils * * @author <a href='mailto:yadong.zhang0415@gmail.com' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' >yadong.zhang</a> * @date 2017年6月1日 下午3:32:37 * @version V1.0 * @since JDK : 1.7 */public class PropertiesListenerConfig { public static Map<String, String> propertiesMap = new HashMap<>(); private static void processProperties(Properties props) throws BeansException { propertiesMap = new HashMap<String, String>(); for (Object key : props.keySet()) { String keyStr = key.toString(); try {// PropertiesLoaderUtils的默認(rèn)編碼是ISO-8859-1,在這里轉(zhuǎn)碼一下propertiesMap.put(keyStr, new String(props.getProperty(keyStr).getBytes('ISO-8859-1'), 'utf-8')); } catch (UnsupportedEncodingException e) {e.printStackTrace(); } catch (java.lang.Exception e) {e.printStackTrace(); } } } public static void loadAllProperties(String propertyFileName) { try { Properties properties = PropertiesLoaderUtils.loadAllProperties(propertyFileName); processProperties(properties); } catch (IOException e) { e.printStackTrace(); } } public static String getProperty(String name) { return propertiesMap.get(name).toString(); } public static Map<String, String> getAllProperty() { return propertiesMap; }}

Applaction.java 啟動(dòng)類

package com.zyd.property;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.zyd.property.config.PropertiesListenerConfig;import com.zyd.property.listener.PropertiesListener;/** * @author <a href='mailto:yadong.zhang0415@gmail.com' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' >yadong.zhang</a> * @date 2017年6月1日 下午3:49:30 * @version V1.0 * @since JDK : 1.7 */@SpringBootApplication@RestControllerpublic class Applaction { /** * * 第四種方式:通過(guò)注冊(cè)監(jiān)聽器(`Listeners`) + `PropertiesLoaderUtils`的方式 * * @author zyd * @throws UnsupportedEncodingException * @since JDK 1.7 */ @RequestMapping('/listener') public Map<String, Object> listener() { Map<String, Object> map = new HashMap<String, Object>(); map.putAll(PropertiesListenerConfig.getAllProperty()); return map; } public static void main(String[] args) throws Exception { SpringApplication application = new SpringApplication(Applaction.class); // 第四種方式:注冊(cè)監(jiān)聽器 application.addListeners(new PropertiesListener('app-config.properties')); application.run(args); }}

訪問(wèn)結(jié)果:

{'com.zyd.name':'zyd','com.zyd.address':'Beijing','com.zyd.title':'使用Listeners + PropertiesLoaderUtils獲取配置文件','com.zyd.type':'Springboot - Listeners','com.zyd.company':'in'}

到此這篇關(guān)于SpringBoot四種讀取properties文件的方式(小結(jié))的文章就介紹到這了,更多相關(guān)SpringBoot讀取properties文件內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
日韩av字幕| 国产精品美女久久久| 日韩一区欧美| 国产成人精品999在线观看| 国产剧情一区| 欧美a级一区二区| 国产亚洲欧美日韩在线观看一区二区| 日韩国产高清在线| 国产精品一区二区三区av麻| 国产精品一区二区免费福利视频| 91精品丝袜国产高跟在线| 青青草国产成人99久久| 欧美日韩午夜| 久久精品国内一区二区三区| 精品中文在线| 日韩免费视频| 韩日一区二区三区| 欧美日韩激情| 亚洲一二av| 日韩精品久久久久久久软件91| 日韩av黄色在线| 久久久国产精品网站| 欧美国产一级| 婷婷综合激情| 亚洲精品免费观看| 国产福利一区二区三区在线播放| 美女精品视频在线| 国产精品99视频| 九色精品91| 日韩激情av在线| 国产一区二区三区四区| 国产91精品对白在线播放| 蜜桃tv一区二区三区| 日韩在线播放一区二区| 国产探花一区| 日韩在线视频精品| 视频一区欧美日韩| 国产精品香蕉| 久久在线免费| 日本精品国产| 欧美成人精品三级网站| 99国产成+人+综合+亚洲欧美| 日韩和欧美的一区| 国产一区二区三区不卡视频网站 | 视频在线在亚洲| 国产日本精品| 亚洲不卡av不卡一区二区| 综合欧美亚洲| 精品欠久久久中文字幕加勒比| 99国产精品一区二区| 香蕉久久久久久| 成人精品高清在线视频| 亚洲欧美视频| 精品免费av一区二区三区| 亚洲大全视频| 国产精品亚洲综合久久| 精品一区欧美| 日韩国产91| 久久精品主播| 国产精品亚洲欧美日韩一区在线| 久久人人88| 国产精品亚洲综合久久| 极品日韩av| 麻豆精品av| 蜜桃av一区二区在线观看| 精品99在线| 日韩美女国产精品| 久久婷婷一区| 国产极品模特精品一二| 国产手机视频一区二区 | 国产日本精品| 成人免费网站www网站高清| 91精品尤物| 99国产精品视频免费观看一公开| 国产激情一区| 亚洲一区av| 亚洲先锋成人| 国产96在线亚洲| 欧美一区成人| 中文精品视频| 欧美香蕉视频| 国产精品色婷婷在线观看| 午夜在线一区| 日韩免费福利视频| 久久不见久久见国语| 中文字幕中文字幕精品| 日韩国产一区二区| 91精品国产自产观看在线| 欧美专区在线| 欧美亚洲激情| 伊人久久视频| 久久99性xxx老妇胖精品| 日韩中文字幕1| 红桃视频国产精品| 青青久久av| 成人国产精品久久| 久久国产人妖系列| 日韩精品一卡二卡三卡四卡无卡| 中文字幕系列一区| 国产66精品| 欧美1区2区3| 国产视频亚洲| 欧美手机在线| 精品丝袜在线| 精品美女视频| 美女av一区| 免费在线日韩av| 欧美一区在线观看视频| 亚洲精品高潮| 免费人成在线不卡| 99国产精品视频免费观看一公开| 久久久久久久久久久妇女 | 亚洲九九精品| 欧美精选一区二区三区| 深夜视频一区二区| 黑人精品一区| 一区二区三区四区日本视频| 欧美激情另类| sm捆绑调教国产免费网站在线观看| 国产精品白丝久久av网站| 国产精品亚洲欧美一级在线 | 蜜臀精品一区二区三区在线观看| 好看的av在线不卡观看| 自拍日韩欧美| 久久亚洲风情| 中文字幕av一区二区三区四区| 免费在线观看成人| 麻豆亚洲精品| 中文字幕亚洲在线观看| 四虎在线精品| 欧美日韩18| 国产高清精品二区| 精品一区二区三区中文字幕视频| 久久麻豆视频| 国产+成+人+亚洲欧洲在线| 福利一区和二区| 欧美aa一级| 欧美va亚洲va日韩∨a综合色| 欧美不卡高清| 中文一区一区三区免费在线观| 亚洲精品伊人| 国产精品亚洲欧美日韩一区在线| 国产免费av一区二区三区| 免费在线欧美黄色| 日韩不卡免费高清视频| 亚洲午夜电影| 一区二区亚洲视频| 国产精品巨作av| 天堂中文av在线资源库| 秋霞国产精品| 日韩精品一级中文字幕精品视频免费观看 | 神马午夜在线视频| 桃色一区二区| 9久re热视频在线精品| 免费成人在线观看| 久久黄色影视| 正在播放日韩精品| 国产一区二区精品| 日韩高清一区| 欧美精品二区| 久久要要av| 日韩欧美精品一区二区综合视频| 国产精品一区三区在线观看| 天堂av在线| 蜜臀91精品一区二区三区| 国产精品亚洲四区在线观看| 中文字幕一区久| 亚洲欧美日韩国产一区| 国产乱码精品一区二区三区四区 | 亚洲+小说+欧美+激情+另类| 国产极品嫩模在线观看91精品| 91视频精品| 蜜桃视频一区二区| 精品视频在线观看网站| 欧美1区免费| 国产精品一国产精品k频道56| 日韩国产一区二区三区| 蜜臀久久久久久久| 国产成人1区| 亚洲一区二区日韩| 成午夜精品一区二区三区软件| 在线亚洲激情| 国产精品久久久久久久久免费高清| 日韩在线看片| 亚洲毛片在线免费| 日韩精品诱惑一区?区三区| 亚洲欧洲午夜| 欧美精品91| 激情久久中文字幕| 国产乱码精品一区二区亚洲| 久久伦理在线| 国产精品任我爽爆在线播放| 精品免费av在线| 久久男人av资源站| 日本久久一区| 五月精品视频| 精品精品99| 亚洲综合五月| 日韩毛片视频| 国产日韩高清一区二区三区在线 |