使用@ConfigurationProperties實(shí)現(xiàn)類型安全的配置過程
@ConfigurationProperties實(shí)現(xiàn)類型安全的配置
問題描述
從之前@Value的使用,可以知道@Value可以靈活的把配置文件中的鍵值對(duì)的值注入到Bean中供我們使用,已經(jīng)很靈活了,但這還不夠,比如下述的application.properties
tomcat.ip=192.168.1.110 tomcat.port=8787 tomcat.projectName=screenshot tomcat.userName=admin tomcat.password=admin
如果也要按照之前的描述,使用@value就要填寫5次,這顯然令人惆悵,在程序員的世界里,所有重復(fù)性的工作都應(yīng)該被取代,因此Spring Boot為我們提供了@ConfigurationProperties注解。
實(shí)踐
application.properties
tomcat.ip=192.168.1.110 tomcat.port=8787 tomcat.projectName=screenshot
從上述的配置項(xiàng)中,可以看到明顯的相似性,即這一簇配置均以tomcat開始,類似String類中的startWith函數(shù)。
核心代碼
package com.wisely.ch6_2_3.config;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "tomcat") //1
@Log4j
public class TomcatSetting {
@Getter
@Setter
private String ip;
@Getter
@Setter
private int port;
@Getter
@Setter
private String projectName;
public String getUrl() {
return "http://"+getIp()+"/"+getPort()+"/"+getProjectName();
}
}
通過這種方法,即可實(shí)現(xiàn)一次性注入相似的配置,非常方便。
減少了重復(fù)性工作,即提高優(yōu)雅度,也能減少錯(cuò)誤,很酷。
關(guān)于ConfigurationProperties注解的說明
下文筆者講述Spring Boot中ConfigurationProperties注解的相關(guān)說明
如下所示:
我們都知道在Spring中,可以使用@Value對(duì)單個(gè)屬性進(jìn)行注入配置操作
但是很多配置都具有一定的層級(jí),那么此時(shí)Spring提供了一個(gè)基于層級(jí)配置的注解
如下所示:
@ConfigurationProperties注解的功能:
- 將properties屬性和一個(gè)Bean及其屬性關(guān)聯(lián)
- 從而實(shí)現(xiàn)類型安全配置
例:
@ConfigurationProperties加載properties文件內(nèi)的配置 使用prefix屬性指定配置文件中定義的properties配置的統(tǒng)一前綴
@ConfigurationProperties(prefix = "remote"}) ?
---Spring Boot1.5之前,使用以下配置指定properties文件的位置
@ConfigurationProperties(prefix = "remote",locations={"classpath:remote.properties"})?示例代碼如下:
remote.address= www.java265.com
remote.port= 9090
?
@Component
@PropertySource({"classpath:remote.properties"})
@ConfigurationProperties(prefix = "remote")
public class RemoteConfig {
? ??
? ? private String address;
? ? private int port;
? ? // getter/stetter方法
}對(duì)應(yīng)RemoteConfig的Bean的使用:
@RestController
public class ConfigurationController {
? ? @Resource
? ? private RemoteConfig remoteConfig;
? ? @GetMapping
? ? public void getInfo() {
? ? ? ? System.out.println("地址:" + remoteConfig.getAddress());
? ? ? ? System.out.println("端口:" + remoteConfig.getPort());
? ? }
}
//測試
@SpringBootTest
@AutoConfigureMockMvc
class ConfigurationControllerTest {
? ? @Autowired
? ? private MockMvc mockMvc;
? ? @Test
? ? void getInfo() throws Exception {
? ? ? ? mockMvc.perform(MockMvcRequestBuilders.get("/"));
? ? }
}
-----運(yùn)行以上代碼,將輸出以下信息------
地址:www.java265.com
端口:9090例:
@ConfigurationProperties注解應(yīng)用于Bean方法上的示例分享
例:
@Configuration
public class MyConfig {
? ? @Bean
? ? @ConfigurationProperties(prefix = "user")
? ? public User user() {
? ? ? ? return new User();
? ? }
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
在JDK和Eclipse下如何編寫和運(yùn)行Java Applet
本文主要介紹了在JDK和Eclipse的環(huán)境下如何編寫和運(yùn)行Java Applet,圖文方式,適合初學(xué)者學(xué)習(xí)。2015-09-09
Java實(shí)現(xiàn)發(fā)送郵件功能時(shí)碰到的坑
之前用163郵箱發(fā)郵件時(shí)明明是成功的,但是使用中國移動(dòng)自己的郵箱時(shí),無論如何在linux服務(wù)器中都發(fā)送不成功。下面小編給大家說下我是怎么解決的,一起看下吧2016-06-06
在spring?boot3中使用native?image的最新方法
這篇文章主要介紹了在spring?boot3中使用native?image?,今天我們用具體的例子來給大家演示一下如何正確的將spring boot3的應(yīng)用編譯成為native image,需要的朋友可以參考下2023-01-01
java中HashSet的特點(diǎn)及實(shí)例用法
在本篇文章里小編給大家整理的是一篇關(guān)于java中HashSet的特點(diǎn)及實(shí)例用法,有興趣的朋友們可以學(xué)習(xí)下。2021-04-04
springboot多數(shù)據(jù)源配合docker部署mysql主從實(shí)現(xiàn)讀寫分離效果
這篇文章主要介紹了springboot多數(shù)據(jù)源配合docker部署mysql主從實(shí)現(xiàn)讀寫分離,通過使用docker獲取mysql鏡像,具體內(nèi)容詳情跟隨小編一起看看吧2021-09-09
Java實(shí)現(xiàn)文件上傳的方法總結(jié)
這篇文章主要為大家介紹了三種Java實(shí)現(xiàn)文件上傳的方法,文中的示例代碼講解詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的借鑒價(jià)值,感興趣的可以了解一下2023-04-04
SpringData JPA中@OneToMany和@ManyToOne的用法詳解
這篇文章主要介紹了SpringData JPA中@OneToMany和@ManyToOne的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

