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)文章
Hibernate+JDBC實現(xiàn)批量插入、更新及刪除的方法詳解
這篇文章主要介紹了Hibernate+JDBC實現(xiàn)批量插入、更新及刪除的方法,結(jié)合實例形式較為詳細的分析了Hibernate與JDBC針對數(shù)據(jù)庫的批量操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-11-11Java使用Jedis操作Redis服務(wù)器的實例代碼
本篇文章主要介紹了Java使用Jedis操作Redis服務(wù)器的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08Mybatis Plus LambdaQueryWrapper的具體用法
Mybatis Plus 在其基礎(chǔ)上擴展了 LambdaQueryWrapper,LambdaQueryWrapper 提供了更加簡便的查詢語法,同時也避免了SQL注入的風險,感興趣的可以了解一下2023-11-11解決logback-classic 使用testCompile的打包問題
這篇文章主要介紹了解決logback-classic 使用testCompile的打包問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07使用maven的profile構(gòu)建不同環(huán)境配置的方法
這篇文章主要介紹了使用maven的profile構(gòu)建不同環(huán)境配置的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01