SpringBoot獲取配置信息的三種方式總結(jié)
更新時(shí)間:2024年01月16日 10:16:53 作者:秋日的晚霞
這篇文章給大家介紹了SpringBoot獲取配置信息的三種方式,@Value屬性值注入,綁定配置類和通過(guò) environment獲取這三種方式,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
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. 通過(guò) 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é)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot獲取配置信息的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Hibernate+JDBC實(shí)現(xiàn)批量插入、更新及刪除的方法詳解
這篇文章主要介紹了Hibernate+JDBC實(shí)現(xiàn)批量插入、更新及刪除的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Hibernate與JDBC針對(duì)數(shù)據(jù)庫(kù)的批量操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-11-11Java使用Jedis操作Redis服務(wù)器的實(shí)例代碼
本篇文章主要介紹了Java使用Jedis操作Redis服務(wù)器的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Mybatis Plus LambdaQueryWrapper的具體用法
Mybatis Plus 在其基礎(chǔ)上擴(kuò)展了 LambdaQueryWrapper,LambdaQueryWrapper 提供了更加簡(jiǎn)便的查詢語(yǔ)法,同時(shí)也避免了SQL注入的風(fēng)險(xiǎn),感興趣的可以了解一下2023-11-11解決logback-classic 使用testCompile的打包問(wèn)題
這篇文章主要介紹了解決logback-classic 使用testCompile的打包問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07使用maven的profile構(gòu)建不同環(huán)境配置的方法
這篇文章主要介紹了使用maven的profile構(gòu)建不同環(huán)境配置的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01SpringMVC如何正確接收時(shí)間的請(qǐng)求示例分析
這篇文章主要為大家介紹了SpringMVC如何正確接收時(shí)間的請(qǐng)求示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09