Spring中的@ConfigurationProperties詳解
@ConfigurationProperties
ConfigurationProperties注解主要用于將外部配置文件配置的屬性填充到這個(gè)** Spring Bean實(shí)例 **中。
需要注意:它自己單獨(dú)使用無效,需要配合其它注解一起使用。且對于Spring Bean才生效,普通的new 對象不生效。
ConfigurationProperties的使用方式:
- @ConfigurationProperties + @Component(或其它實(shí)例化Bean的注解)注解到bean定義類上
- @ConfigurationProperties + @Bean注解到配置類的bean定義方法上
- @ConfigurationProperties注解到普通類,然后通過@EnableConfigurationProperties定義為bean
使用方式
配置文件:
sftp: host: 127.0.0.1 port: 22 username: admin password: 123456
1. @ConfigurationProperties + @Component
@ConfigurationProperties + @Component(或其它實(shí)例化Bean的注解)注解到bean定義類上
@Data
@Component
// @Configuration
@ConfigurationProperties(prefix = "sftp")
public class Sftp {
private String host;
private String port;
private String username;
private String password;
}2. @ConfigurationProperties + @Bean
@ConfigurationProperties + @Bean注解到配置類的bean定義方法上
@Data
public class Sftp {
private String host;
private String port;
private String username;
private String password;
}@Configuration
public class BeanConfig {
@Bean
@ConfigurationProperties(prefix = "sftp")
public Sftp sftp() {
return new Sftp();
}
}此種用法遇到的坑:重復(fù)使用@ConfigurationProperties 如果將代碼改成這樣:
@Data
@ConfigurationProperties(prefix = "sftp")
public class Sftp {
private String host;
private String port;
private String username;
private String password;
}@Configuration
public class BeanConfig {
@Bean
@ConfigurationProperties(prefix = "aaaa")
public Sftp sftp() {
return new Sftp();
}
}這種方式上有@ConfigurationProperties注解,且類上也有@ConfigurationProperties注解的,只有方法上的@ConfigurationProperties會生效(可以理解為類上的注解方法的給重寫了)。
3. @ConfigurationProperties + @EnableConfigurationProperties
@ConfigurationProperties注解到普通類,然后通過@EnableConfigurationProperties定義為bean
@Data
@ConfigurationProperties(prefix = "sftp")
public class Sftp {
private String host;
private String port;
private String username;
private String password;
}@SpringBootApplication
@EnableConfigurationProperties(value = {Sftp.class})
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}到此這篇關(guān)于Spring中的@ConfigurationProperties詳解的文章就介紹到這了,更多相關(guān)@ConfigurationProperties詳解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot使用POI進(jìn)行excel文件的導(dǎo)出與下載方式
這篇文章主要介紹了Springboot使用POI進(jìn)行excel文件的導(dǎo)出與下載方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
java JDBC主要組件連接數(shù)據(jù)庫及執(zhí)行SQL過程示例全面詳解
這篇文章主要為大家介紹了java JDBC主要組件連接數(shù)據(jù)庫及執(zhí)行SQL的過程示例全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine(推薦)
這篇文章主要介紹了Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
SpringBoot創(chuàng)建線程池的六種方式小結(jié)
本文主要介紹了SpringBoot創(chuàng)建線程池的六種方式小結(jié),包括自定義線程池,固定長度線程池,單一線程池,共享線程池,定時(shí)線程池,SpringBoot中注入異步線程池,感興趣的可以了解一下2023-11-11
Java多線程之Semaphore實(shí)現(xiàn)信號燈
這篇文章主要給大家分享的是Java多線程之Semaphore實(shí)現(xiàn)信號燈的練習(xí),emaphore是計(jì)數(shù)信號量。Semaphore管理一系列許可證。每個(gè)acquire方法阻塞,直到有一個(gè)許可證可以獲得然后拿走一個(gè)許可證;下面一起進(jìn)入文章學(xué)習(xí)Semaphore的具體內(nèi)容2021-10-10
idea配置spring項(xiàng)目ApplicationContext.xml自動生成實(shí)踐
本文簡述Spring配置步驟:通過文件設(shè)置添加配置項(xiàng),創(chuàng)建applicationContext.xml文件,并驗(yàn)證配置成功,為個(gè)人經(jīng)驗(yàn)總結(jié),供開發(fā)者參考2025-09-09
POI XSSFSheet shiftRows bug問題解決
這篇文章主要介紹了POI XSSFSheet shiftRows bug問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

