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

您的位置:首頁技術文章
文章詳情頁

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

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

前言

在項目開發中經常會用到配置文件,配置文件的存在解決了很大一份重復的工作。今天就分享四種在Springboot中獲取配置文件的方式。

注:前三種測試配置文件為springboot默認的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;/** * 對應上方配置文件中的第一段配置 * @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默認取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; }}

程序啟動類: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`注解將配置文件屬性注入到配置對象類中 * * @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); }}

訪問結果:

{'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注解方式

程序啟動類: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文件中的中文默認以ISO-8859-1方式編碼,因此需要對中文內容進行重新編碼 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); }}

訪問結果:

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

三、使用Environment

程序啟動類: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); }}

訪問結果:

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

四、使用PropertiesLoaderUtils

app-config.properties

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

PropertiesListener.java 用來初始化加載配置文件

package com.zyd.property.listener;import org.springframework.boot.context.event.ApplicationStartedEvent;import org.springframework.context.ApplicationListener;import com.zyd.property.config.PropertiesListenerConfig;/** * 配置文件監聽器,用來加載自定義配置文件 * * @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 加載配置文件內容

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的默認編碼是ISO-8859-1,在這里轉碼一下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 啟動類

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 { /** * * 第四種方式:通過注冊監聽器(`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); // 第四種方式:注冊監聽器 application.addListeners(new PropertiesListener('app-config.properties')); application.run(args); }}

訪問結果:

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

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

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
欧美福利专区| 奇米色欧美一区二区三区| 7m精品国产导航在线| 日韩欧美高清一区二区三区| 久久国产精品成人免费观看的软件| 视频一区二区国产| 999久久久亚洲| 蜜桃成人精品| 群体交乱之放荡娇妻一区二区| 麻豆国产精品视频| 精品美女视频| 国产成人精品亚洲日本在线观看| 日韩成人高清| 一本一本久久| 亚洲精品自拍| 国产极品久久久久久久久波多结野 | 亚洲一区免费| 日韩专区欧美专区| 日韩国产一二三区| 欧美国产另类| av中文字幕在线观看第一页| 亚洲成人va| 欧美不卡高清| 蜜臀久久久99精品久久久久久| 亚洲精品影视| 美女毛片一区二区三区四区最新中文字幕亚洲 | 国产麻豆久久| 亚洲综合日本| 777久久精品| 国产一区二区三区四区二区 | 久久久一二三| 午夜在线精品偷拍| 日韩激情一二三区| 国产一区二区三区不卡视频网站 | 国产欧美亚洲精品a| 久久精品女人| 亚洲欧美日韩高清在线| 亚洲人成高清| 国内精品伊人| 欧美 日韩 国产精品免费观看| 久久国产精品久久久久久电车 | 福利视频一区| 亚洲在线成人| 国产精品久久国产愉拍| 日韩在线短视频| 亚洲在线观看| 国产高清精品二区| 欧美成人基地 | 久久亚州av| 亚洲一级黄色| 欧美久久久网站| 精品丝袜在线| 日本午夜免费一区二区| 97se综合| 日本在线视频一区二区| 荡女精品导航| 一区二区三区四区日韩| 久久av偷拍| 悠悠资源网久久精品| 国产日韩欧美三级| 欧美亚洲国产一区| 久久激情五月婷婷| 欧美中文一区二区| 亚洲精品极品| 久久久久久黄| 国产精品视频一区二区三区四蜜臂| 欧美三级精品| 欧美日韩精品一区二区三区视频 | 精品在线99| 亚洲久久在线| 免费在线亚洲欧美| 欧美日韩国产探花| 国产精品亚洲欧美| 午夜久久免费观看| 麻豆精品新av中文字幕| 91高清一区| 精品一区av| 亚洲在线久久| 国产亚洲一区二区手机在线观看| 亚洲精品精选| 婷婷精品视频| 欧美激情aⅴ一区二区三区 | 亚洲激情二区| 久久免费影院| 亚洲精品乱码| 激情综合网站| 国产一区二区三区四区五区传媒| 亚洲精品黄色| 婷婷精品进入| 日韩成人综合| 欧美精品二区| 日韩精品乱码av一区二区| 日韩精品一卡| 精品色999| 色综合视频一区二区三区日韩| 日韩欧美一区二区三区在线观看 | 成人日韩在线| 国产精品白丝一区二区三区| 蜜桃久久精品一区二区| 久久久久久久久久久9不雅视频| 国产精品调教| 日本不卡视频一二三区| 午夜影院欧美| 偷拍精品精品一区二区三区| 国产剧情一区二区在线观看| 在线观看一区| 午夜精品免费| 成人羞羞视频在线看网址| 麻豆国产精品777777在线| 亚洲综合婷婷| 视频一区二区不卡| 伊人精品视频| 激情91久久| 一区二区视频欧美| 欧美网站在线| 欧美成人午夜| 日韩精品欧美| 伊人久久高清| 四虎4545www国产精品| 岛国av在线播放| 精品久久久亚洲| 欧美91在线| 久久精品欧洲| 福利一区二区| 精品国产99| 国产专区精品| 国内不卡的一区二区三区中文字幕| 国产剧情一区| 国产精品18| 欧美韩一区二区| 国产精品久久国产愉拍| 国产精品传媒麻豆hd| 久久99影视| 国产中文字幕一区二区三区| 电影91久久久| 亚洲黄色免费看| 久久影院一区| 欧美91视频| 香蕉精品999视频一区二区| 免费观看久久av| 香蕉精品999视频一区二区| 日本 国产 欧美色综合| 亚洲影院天堂中文av色| 视频精品一区| 日韩高清不卡在线| 国产欧美日韩精品一区二区免费 | 国产麻豆精品| 另类欧美日韩国产在线| 成人国产综合| 在线视频观看日韩| 国产亚洲毛片| 日韩中文av| 日本不卡中文字幕| 国产精品主播| 首页国产精品| 免费不卡中文字幕在线| 色综合视频一区二区三区日韩| 国产欧美日韩在线一区二区 | 日本伊人久久| 国产激情综合| 三级精品视频| 久久亚洲图片| 欧美综合社区国产| 国产精品最新| 日韩精品网站| 蜜桃久久久久久| 另类小说一区二区三区| 欧洲一级精品| 免费在线看一区| 国产日产一区| 成人av三级| 蜜桃av一区| 国产精品欧美三级在线观看| 亚洲啊v在线| 亚洲欧洲美洲国产香蕉| 国产精品va| 色老板在线视频一区二区| 日韩一区精品视频| 国产日产一区| 国产99精品| 日韩精品中文字幕一区二区| 国产videos久久| 蘑菇福利视频一区播放| 国产精品对白| re久久精品视频| 欧美日韩视频免费看| 亚洲不卡系列| 青青草国产成人99久久| 日韩一区欧美| 日韩超碰人人爽人人做人人添| аⅴ资源天堂资源库在线| 手机精品视频在线观看| 精品视频自拍| 女人天堂亚洲aⅴ在线观看| 欧美日韩1区2区3区| 婷婷国产精品| 久久久免费人体| 免费欧美在线视频| 亚洲美女久久精品| 91麻豆精品|