一般而言,SpringBoot項目中都會有一個默認的配置文件Application.Properties,相當於一個變量的列表,有點類似於系統啓動時的*.ini文件,一般都是放在Resources文件夾下面。
那麼在程序中,又是怎麼讀取和使用Application.Properties裏面的配置信息的呢?一般有三種方式:
1. @Value
一般在程序啓動類裏使用,相當於聲明和初始化了某個屬性。
Application.Properties配置文件信息如下:
com.zyd.type=Springboot - @Value
com.zyd.title=使用@Value獲取配置文件
在啓動類中加入@Value("${com.zyd.type}")
public class Applaction {
@Value("${com.zyd.type}")
private String type;
@Value("${com.zyd.title}")
private String title;
@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);
}
}
2. Environment
一般在程序啓動類裏使用,相當於通過這個類的方法獲取到屬性信息。
Application.Properties配置文件信息如下:
com.zyd.type=Springboot - @Value
com.zyd.title=使用@Value獲取配置文件
在啓動類中可以使用Environment獲取配置信息。
....
import org.springframework.core.env.Environment;
....
public class Applaction {
@Autowired
private Environment env;
@RequestMapping("/env")
public Map<String, Object> env() throws UnsupportedEncodingException {
Map<String, Object> map = new HashMap<String, Object>();
map.put("type", env.getProperty("com.zyd.type"));
map.put("title", new String(env.getProperty("com.zyd.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);
}
}
3. @ConfigurationProperties
一般在自定義類裏使用,相當於通過自定義類的方法獲取到屬性信息,前提是要在自定義類中設置相應的方法獲取配置信息。
自定義類
public class PropertiesConfig {
public String type;
public String title;
public Map<String, String> login = new HashMap<String, String>();
public List<String> urls = new ArrayList<>();
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTitle() {
try {
return new String(title.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return title;
}
public void setTitle(String title) {
this.title = title;
}
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;
}
}
使用自定義類讀取配置信息
....
import com.zyd.property.config.PropertiesConfig;
....
public class Applaction {
@Autowired
private PropertiesConfig propertiesConfig;
@RequestMapping("/config")
public Map<String, Object> configurationProperties() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("type", propertiesConfig.getType());
map.put("title", propertiesConfig.getTitle());
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);
}
}
當然,以上的讀取的都是默認的配置文件,還可以通過加入註冊監聽器的方式加載配置文件,即是PropertiesLoaderUtils方法。