Spring 開發(fā)之組件賦值的實現方法
1. @Value & @PropertySource
1.1 使用方式
@PropertySource:讀取外部配置文件中的 k/v 保存到運行的環(huán)境變量中;加載完外部的配置文件以后使用 ${} 取出配置文件的值
@Value:賦值
- 基本數值
- 可以寫 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)境標識的 bean,只有這個環(huán)境被激活的時候才能注冊到容器中。默認是 default 環(huán)境
寫在配置類上,只有是指定的環(huán)境的時候,整個配置類里面的所有配置才能開始生效
沒有標注環(huán)境標識的 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:設置虛擬機參數 -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. 設置需要激活的環(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
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring應用中使用acutator/refresh刷新屬性不生效的問題分析及解決
在Spring應用收到/actuator/refresh的POST請求后,標注了@RefreshScope以及@ConfiguratioinProperties的bean會被Spring容器重新加載,但是,在實際應用中,并沒有按照預期被Spring容器加載,本文將討論導致這種未按預期刷新的一種原因,感興趣的朋友可以參考下2024-01-01
springboot vue完成編輯頁面發(fā)送接口請求功能
這篇文章主要為大家介紹了springboot+vue完成編輯頁發(fā)送接口請求功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
Java 將List中的實體類按照某個字段進行分組并存放至Map中操作
這篇文章主要介紹了Java 將List中的實體類按照某個字段進行分組并存放至Map中操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10

