Spring 開發(fā)之組件賦值的實現(xiàn)方法
1. @Value & @PropertySource
1.1 使用方式
@PropertySource:讀取外部配置文件中的 k/v 保存到運行的環(huán)境變量中;加載完外部的配置文件以后使用 ${} 取出配置文件的值
@Value:賦值
- 基本數(shù)值
- 可以寫 SpEL,#{}
- 可以寫 ${};取出配置文件【properties】中的值(在運行環(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:指定組件在哪個環(huán)境的情況下才能被注冊到容器中,不指定,任何環(huán)境下都能注冊這個組件
加了環(huán)境標(biāo)識的 bean,只有這個環(huán)境被激活的時候才能注冊到容器中。默認(rèn)是 default 環(huán)境
寫在配置類上,只有是指定的環(huán)境的時候,整個配置類里面的所有配置才能開始生效
沒有標(biāo)注環(huán)境標(biāo)識的 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è)置虛擬機參數(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)建一個 AnnotationConfigApplicationContext AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); //2. 設(shè)置需要激活的環(huán)境 context.getEnvironment().setActiveProfiles("dev", "prod"); //3. 注冊主配置類 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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java編程隊列數(shù)據(jù)結(jié)構(gòu)代碼示例
這篇文章主要介紹了java編程隊列數(shù)據(jù)結(jié)構(gòu)代碼示例,簡單介紹了隊列的相關(guān)基礎(chǔ)知識,然后通過實例向大家展示其實現(xiàn)方法,具有一定參考價值,需要的朋友可以了解下。2017-11-11Spring應(yīng)用中使用acutator/refresh刷新屬性不生效的問題分析及解決
在Spring應(yīng)用收到/actuator/refresh的POST請求后,標(biāo)注了@RefreshScope以及@ConfiguratioinProperties的bean會被Spring容器重新加載,但是,在實際應(yīng)用中,并沒有按照預(yù)期被Spring容器加載,本文將討論導(dǎo)致這種未按預(yù)期刷新的一種原因,感興趣的朋友可以參考下2024-01-01深入理解SpringMVC的參數(shù)綁定與數(shù)據(jù)響應(yīng)機制
本文將深入探討SpringMVC的參數(shù)綁定方式,包括基本類型、對象、集合等類型的綁定方式,以及如何處理參數(shù)校驗和異常。同時,本文還將介紹SpringMVC的數(shù)據(jù)響應(yīng)機制,包括如何返回JSON、XML等格式的數(shù)據(jù),以及如何處理文件上傳和下載。2023-06-06springboot vue完成編輯頁面發(fā)送接口請求功能
這篇文章主要為大家介紹了springboot+vue完成編輯頁發(fā)送接口請求功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05java啟動jar包設(shè)置啟動參數(shù)的實現(xiàn)
本文主要介紹了java啟動jar包設(shè)置啟動參數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java 將List中的實體類按照某個字段進行分組并存放至Map中操作
這篇文章主要介紹了Java 將List中的實體類按照某個字段進行分組并存放至Map中操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10