前言
在项目开发中经常会用到配置文件,配置文件的存在解决了很大一份重复的工作。今天就分享四种在Springboot中获取配置文件的方式。
注:前三种测试配置文件为springboot默认的application.properties
文件
#######################方式一#########################
com.battle.type3=Springboot - @ConfigurationProperties
com.battle.title3=使用@ConfigurationProperties获取配置文件
#map
com.battle.login[username]=admin
com.battle.login[password]=123456
com.battle.login[callback]=http://www.flyat.cc
#list
com.battle.urls[0]=http://ztool.cc
com.battle.urls[1]=http://ztool.cc/format/js
com.battle.urls[2]=http://ztool.cc/str2image
com.battle.urls[3]=http://ztool.cc/json2Entity
com.battle.urls[4]=http://ztool.cc/ua
#######################方式二#########################
com.battle.type=Springboot - @Value
com.battle.title=使用@Value获取配置文件
#######################方式三#########################
com.battle.type2=Springboot - Environment
com.battle.title2=使用Environment获取配置文件
一、@ConfigurationProperties方式
自定义配置类:PropertiesConfig.java
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.u