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)境標識的 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:設(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java課程設(shè)計做一個多人聊天室(socket+多線程)
這篇文章主要介紹了我的java課程設(shè)計一個多人聊天室(socket+多線程)本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
java多線程之wait(),notify(),notifyAll()的詳解分析
本篇文章是對java多線程 wait(),notify(),notifyAll()進行了詳細的分析介紹,需要的朋友參考下2013-06-06
利用ScriptEngineManager實現(xiàn)字符串公式靈活計算的方法
今天小編就為大家分享一篇利用ScriptEngineManager實現(xiàn)字符串公式靈活計算的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

