Spring @Environment典型用法實(shí)戰(zhàn)案例
簡(jiǎn)單說(shuō):Spring 里沒(méi)有直接叫 @Environment 的注解,更準(zhǔn)確說(shuō)常用的是 @Autowired 注入 Environment 對(duì)象,或者結(jié)合 @Value 配合 Environment 讀取配置 。
支持從以下來(lái)源讀取:
1、application.properties / .yaml
2、JVM 參數(shù)(如 -Dkey=value)
3、系統(tǒng)環(huán)境變量
4、自定義 @PropertySource
獲取Environment實(shí)例
使用 @Autowired 注入 Environment 接口實(shí)例
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class MyComponent { @Autowired private Environment environment; // ? 正確方式 // 使用 environment 對(duì)象獲取屬性或 profile }
結(jié)合 @PropertySource 使用示例
@Configuration @PropertySource("classpath:custom.properties") public class AppConfig { @Autowired private Environment env; @Bean public MyService myService() { String customValue = env.getProperty("custom.key"); return new MyService(customValue); } }
使用Environment實(shí)例
獲取配置屬性值
String dbUrl = environment.getProperty("spring.datasource.url"); String appName = environment.getProperty("app.name", "defaultAppName"); // 默認(rèn)值
獲取類(lèi)型安全的屬性值
Boolean featureEnabled = environment.getProperty("feature.enabled", Boolean.class, false);
獲取當(dāng)前激活的 Profile
String[] activeProfiles = environment.getActiveProfiles(); for (String profile : activeProfiles) { System.out.println("Active Profile: " + profile); }
判斷是否匹配某個(gè) Profile
if (environment.acceptsProfiles("dev")) { System.out.println("Running in development mode."); } // 或者使用更現(xiàn)代的寫(xiě)法: if (environment.acceptsProfiles(Profiles.of("dev"))) { // dev 環(huán)境邏輯 }
獲取所有 PropertySources(調(diào)試用途)
import org.springframework.core.env.AbstractEnvironment; import org.springframework.core.env.PropertySource; ((AbstractEnvironment) environment).getPropertySources().forEach(ps -> { System.out.println("Property Source: " + ps.getName()); });
常見(jiàn) PropertySource 包括:
1、systemProperties(JVM 參數(shù))
2、systemEnvironment(操作系統(tǒng)環(huán)境變量)
3、servletConfigInitParams(Web 配置參數(shù))
4、自定義加載的 @PropertySource
總結(jié)
到此這篇關(guān)于Spring @Environment典型用法的文章就介紹到這了,更多相關(guān)Spring @Environment用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis mapper互相引用resultMap啟動(dòng)出錯(cuò)的解決
這篇文章主要介紹了mybatis mapper互相引用resultMap啟動(dòng)出錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java的線(xiàn)程池ThreadPoolExecutor及多種線(xiàn)程池實(shí)現(xiàn)詳解
這篇文章主要介紹了Java的線(xiàn)程池ThreadPoolExecutor及多種線(xiàn)程池實(shí)現(xiàn)詳解,ThreadPoolExecutor 使用 int 的高 3 位來(lái)表示線(xiàn)程池狀態(tài),低 29 位表示線(xiàn)程數(shù)量,之所以將信息存儲(chǔ)在一個(gè)變量中,是為了保證原子性,需要的朋友可以參考下2024-01-01Spring?EnableAsync注解異步執(zhí)行源碼解析
這篇文章主要為大家介紹了Spring?EnableAsync注解源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之漢諾塔
漢諾塔是源于印度一個(gè)古老傳說(shuō)的益智玩具。大梵天創(chuàng)造世界時(shí)做了三根石柱,在一根柱子上從下往上按大小順序摞著64片黃金圓盤(pán)。大梵天命令婆羅門(mén)把圓盤(pán)從下面開(kāi)始按大小順序重新擺放在另一根柱子上。并且規(guī)定,在小圓盤(pán)上不能放大圓盤(pán),三根柱子之間一次只能移動(dòng)一個(gè)圓盤(pán)2022-02-02Java?Stream排序的實(shí)現(xiàn)方式面試精講
這篇文章主要為大家介紹了Java?Stream排序的實(shí)現(xiàn)方式面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09java中的Integer的toBinaryString()方法實(shí)例
這篇文章主要介紹了java中的Integer的toBinaryString()方法實(shí)例,有需要的朋友可以參考一下2013-12-12