使用@ConfigurationProperties實(shí)現(xiàn)類型安全的配置過(guò)程
@ConfigurationProperties實(shí)現(xiàn)類型安全的配置
問(wè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開(kāi)始,類似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();
}
}
通過(guò)這種方法,即可實(shí)現(xiàn)一次性注入相似的配置,非常方便。
減少了重復(fù)性工作,即提高優(yōu)雅度,也能減少錯(cuò)誤,很酷。
關(guān)于ConfigurationProperties注解的說(shuō)明
下文筆者講述Spring Boot中ConfigurationProperties注解的相關(guān)說(shuō)明
如下所示:
我們都知道在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());
? ? }
}
//測(cè)試
@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)文章
基于SpringBoot實(shí)現(xiàn)發(fā)送帶附件的郵件
這篇文章主要介紹了基于SpringBoot實(shí)現(xiàn)發(fā)送帶附件的郵件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
超簡(jiǎn)單的java獲取鼠標(biāo)點(diǎn)擊位置坐標(biāo)的實(shí)例(鼠標(biāo)在Jframe上的坐標(biāo))
在Java窗體Jframe上獲取鼠標(biāo)點(diǎn)擊的坐標(biāo),其中使用了匿名內(nèi)部類,實(shí)例代碼非常簡(jiǎn)單易懂,大家可以學(xué)習(xí)一下2018-03-03
mybatis的association傳遞參數(shù)問(wèn)題示例
這篇文章主要介紹了mybatis的association傳遞參數(shù)問(wèn)題,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
java實(shí)現(xiàn)TCP socket和UDP socket的實(shí)例
這篇文章主要介紹了本文主要介紹了java實(shí)現(xiàn)TCP socket和UDP socket的實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

