Spring 開發(fā)之組件賦值的實(shí)現(xiàn)方法
1. @Value & @PropertySource
1.1 使用方式
@PropertySource:讀取外部配置文件中的 k/v 保存到運(yùn)行的環(huán)境變量中;加載完外部的配置文件以后使用 ${} 取出配置文件的值
@Value:賦值
- 基本數(shù)值
- 可以寫 SpEL,#{}
- 可以寫 ${};取出配置文件【properties】中的值(在運(yùn)行環(huán)境變量里面的值)
1.2 代碼
1. Person
@Data @Slf4j @ToString public class Person { @Value("#{001+001}") private Long id; @Value("zs") private String name; @Value("${person.address}") private String address; @Value("${person.age:19}") private Integer age; }
2. SpringConfig
@Configuration @PropertySource(value = "classpath:person.properties") public class SpringConfig { @Bean public Person person() { return new Person(); } }
3. PropertiesTest
public class PropertiesTest { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); Person person = context.getBean(Person.class); System.out.println(person); //Person(id=2, name=zs, address=上海市, age=19) } }
2. @Profile
2.1 使用方式
@Profile:指定組件在哪個(gè)環(huán)境的情況下才能被注冊(cè)到容器中,不指定,任何環(huán)境下都能注冊(cè)這個(gè)組件
加了環(huán)境標(biāo)識(shí)的 bean,只有這個(gè)環(huán)境被激活的時(shí)候才能注冊(cè)到容器中。默認(rèn)是 default 環(huán)境
寫在配置類上,只有是指定的環(huán)境的時(shí)候,整個(gè)配置類里面的所有配置才能開始生效
沒有標(biāo)注環(huán)境標(biāo)識(shí)的 bean 在任何環(huán)境下都是加載的
2.2 代碼
1. Message
@Data @NoArgsConstructor @AllArgsConstructor public class Message { private String label = null; }
2. SpringConfig
@Configuration public class SpringConfig { @Profile("default") @Bean public Message defaultMessage() { return new Message("default"); } @Profile("dev") @Bean public Message devMessage() { return new Message("dev"); } @Profile("test") @Bean public Message testMessage() { return new Message("test"); } @Profile("prod") @Bean public Message prodMessage() { return new Message("prod"); } }
3. 激活方式 1
激活 profile:設(shè)置虛擬機(jī)參數(shù) -Dspring.profiles.active=dev,test
public class ProfileTest { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); String[] names = context.getBeanNamesForType(Message.class); for (String name : names) { System.out.println(name); } //devMessage //prodMessage } }
4. 激活方式 2
public class ProfileTest { public static void main(String[] args) { //1. 創(chuàng)建一個(gè) AnnotationConfigApplicationContext AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); //2. 設(shè)置需要激活的環(huán)境 context.getEnvironment().setActiveProfiles("dev", "prod"); //3. 注冊(cè)主配置類 context.register(SpringConfig.class); //4. 刷新容器 context.refresh(); String[] names = context.getBeanNamesForType(Message.class); for (String name : names) { System.out.println(name); } //testMessage //prodMessage } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java編程隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼示例
這篇文章主要介紹了java編程隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼示例,簡單介紹了隊(duì)列的相關(guān)基礎(chǔ)知識(shí),然后通過實(shí)例向大家展示其實(shí)現(xiàn)方法,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11Spring應(yīng)用中使用acutator/refresh刷新屬性不生效的問題分析及解決
在Spring應(yīng)用收到/actuator/refresh的POST請(qǐng)求后,標(biāo)注了@RefreshScope以及@ConfiguratioinProperties的bean會(huì)被Spring容器重新加載,但是,在實(shí)際應(yīng)用中,并沒有按照預(yù)期被Spring容器加載,本文將討論導(dǎo)致這種未按預(yù)期刷新的一種原因,感興趣的朋友可以參考下2024-01-01Java之獲取客戶端真實(shí)IP地址的實(shí)現(xiàn)
在開發(fā)工作中,我們常常需要獲取客戶端的IP,本文主要介紹了Jav之獲取客戶端真實(shí)IP地址的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12深入理解SpringMVC的參數(shù)綁定與數(shù)據(jù)響應(yīng)機(jī)制
本文將深入探討SpringMVC的參數(shù)綁定方式,包括基本類型、對(duì)象、集合等類型的綁定方式,以及如何處理參數(shù)校驗(yàn)和異常。同時(shí),本文還將介紹SpringMVC的數(shù)據(jù)響應(yīng)機(jī)制,包括如何返回JSON、XML等格式的數(shù)據(jù),以及如何處理文件上傳和下載。2023-06-06springboot vue完成編輯頁面發(fā)送接口請(qǐng)求功能
這篇文章主要為大家介紹了springboot+vue完成編輯頁發(fā)送接口請(qǐng)求功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05java啟動(dòng)jar包設(shè)置啟動(dòng)參數(shù)的實(shí)現(xiàn)
本文主要介紹了java啟動(dòng)jar包設(shè)置啟動(dòng)參數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java 將List中的實(shí)體類按照某個(gè)字段進(jìn)行分組并存放至Map中操作
這篇文章主要介紹了Java 將List中的實(shí)體類按照某個(gè)字段進(jìn)行分組并存放至Map中操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10