SpringBoot獲取配置信息的三種方式總結(jié)
更新時間:2024年01月16日 10:16:53 作者:秋日的晚霞
這篇文章給大家介紹了SpringBoot獲取配置信息的三種方式,@Value屬性值注入,綁定配置類和通過 environment獲取這三種方式,文中通過代碼示例給大家介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下
Spring獲取配置信息的三種方式
1. @Value屬性值注入
@Value("${student.name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(SpringConfigDemoApplication.class, args);
}
@PostConstruct
private void init()
{
System.out.println("name = " + name);
}
2. 綁定配置類
@ConfigurationProperties(prefix = "student")
public class StudentProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@EnableConfigurationProperties(StudentProperties.class)
@SpringBootApplication
public class SpringConfigDemoApplication {
@Value("${student.name}")
private String name;
@Autowired
private StudentProperties studentProperties;
public static void main(String[] args) {
SpringApplication.run(SpringConfigDemoApplication.class, args);
}
@PostConstruct
private void init()
{
System.out.println("name1 = " + name);
}
@PostConstruct
private void init2()
{
System.out.println("name2 = " +studentProperties.getName());
}
}
3. 通過 environment 獲取
package com.sz.springconfigdemo;
import com.sz.springconfigdemo.properties.StudentProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import javax.annotation.PostConstruct;
@EnableConfigurationProperties(StudentProperties.class)
@SpringBootApplication
public class SpringConfigDemoApplication {
@Value("${student.name}")
private String name;
@Autowired
private StudentProperties studentProperties;
@Autowired
private Environment environment;
public static void main(String[] args) {
SpringApplication.run(SpringConfigDemoApplication.class, args);
}
@PostConstruct
private void init()
{
System.out.println("name1 = " + name);
}
@PostConstruct
private void init2()
{
System.out.println("name2 = " +studentProperties.getName());
}
@PostConstruct
private void init3()
{
String environmentProperty = environment.getProperty("student.name");
System.out.println("name3 = " +environmentProperty);
}
}
以上就是SpringBoot獲取配置信息的三種方式總結(jié)的詳細內(nèi)容,更多關(guān)于SpringBoot獲取配置信息的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
IDEA SpringBoot項目配置熱更新的步驟詳解(無需每次手動重啟服務(wù)器)
這篇文章主要介紹了IDEA SpringBoot項目配置熱更新的步驟,無需每次手動重啟服務(wù)器,本文通過圖文實例代碼相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
SpringBoot整合Shiro靜態(tài)資源攔截配置實踐
本文介紹了Spring?Boot整合Thymeleaf訪問resources目錄下static靜態(tài)資源的方法,并提供三種解決方式,方法一通過修改訪問路徑,方法二在yml文件中修改配置,方法三通過創(chuàng)建statics目錄并調(diào)整攔截配置來實現(xiàn)2026-05-05
java實現(xiàn)解析json復(fù)雜數(shù)據(jù)的方法詳解
這篇文章主要為大家詳細介紹了java如何實現(xiàn)解析json復(fù)雜數(shù)據(jù),文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以學(xué)習(xí)一下2024-01-01

