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)境的情況下才能被注冊到容器中,不指定,任何環(huán)境下都能注冊這個(gè)組件
加了環(huán)境標(biāo)識的 bean,只有這個(gè)環(huán)境被激活的時(shí)候才能注冊到容器中。默認(rèn)是 default 環(huán)境
寫在配置類上,只有是指定的環(huán)境的時(shí)候,整個(gè)配置類里面的所有配置才能開始生效
沒有標(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è)置虛擬機(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. 注冊主配置類
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ì)做一個(gè)多人聊天室(socket+多線程)
這篇文章主要介紹了我的java課程設(shè)計(jì)一個(gè)多人聊天室(socket+多線程)本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Java中的Cglib動(dòng)態(tài)代理詳細(xì)解讀
這篇文章主要介紹了Java中的Cglib動(dòng)態(tài)代理詳細(xì)解讀,CGLib是一個(gè)強(qiáng)大的、高性能、高質(zhì)量的 Code 生成類庫,它可以在運(yùn)行期擴(kuò)展 Java 類與實(shí)現(xiàn) Java 接口,需要的朋友可以參考下2023-11-11
java多線程之wait(),notify(),notifyAll()的詳解分析
本篇文章是對java多線程 wait(),notify(),notifyAll()進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
利用ScriptEngineManager實(shí)現(xiàn)字符串公式靈活計(jì)算的方法
今天小編就為大家分享一篇利用ScriptEngineManager實(shí)現(xiàn)字符串公式靈活計(jì)算的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
java操作Redis緩存設(shè)置過期時(shí)間的方法
這篇文章主要介紹了java操作Redis緩存設(shè)置過期時(shí)間的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
使用Spring boot標(biāo)記一個(gè)方法過時(shí)
這篇文章主要介紹了使用Spring boot標(biāo)記一個(gè)方法過時(shí),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12

