1.自定義復(fù)雜嵌套配置信息類
??上一節(jié)中,我給大家介紹了簡(jiǎn)單類如何自定義配置信息,但是實(shí)際應(yīng)用場(chǎng)景中可能你并不希望把所有的配置信息都放在一個(gè)類中,使得一個(gè)類太臃腫,我們希望分離這些配置信息,所以類中常常會(huì)嵌套其他類,例如
@ConfigurationProperties(prefix = "wyx.security")
public class SecurityProperties {
private BrowserProperties browser = new BrowserProperties();
private ImageCodeProperties imgCode = new ImageCodeProperties();
//省略get,set方法
}
public class BrowserProperties {
private String loginPage ;
private LoginType loginType;
//省略get,set方法
}
public class ImageCodeProperties {
private int width=67;
private int height=23;
private int length=4;
private int expireIn=60;
private String urls;
//省略get,set方法
}
??這么做是沒(méi)問(wèn)題的,spring依然會(huì)為我們裝配這些配置信息,配置如下
application.yml
wyx:
security:
browser:
login-page: /demo-signIn.html
login-type: redirect
img-code:
length: 20
urls: /user,/user/*
??這時(shí)候我們發(fā)現(xiàn)IDEA又無(wú)法正確提示配置信息了,通過(guò)上一節(jié)我們了解到,IDEA主要是依靠spring-configuration-metadata.json文件去提示配置信息,也就是說(shuō)我們現(xiàn)在生成的json文件不正確,我們來(lái)看看
spring-configuration-metadata.json
{
"hints": [],
"groups": [
{
"sourceType": "wyx.practice.spring.security.core.properties.SecurityProperties",
"name": "wyx.security",
"type": "wyx.practice.spring.security.core.properties.SecurityProperties"
}
],
"properties": [
{
"sourceType": "wyx.practice.spring.security.core.properties.SecurityProperties",
"name": "wyx.security.browser",
"type": "wyx.practice.spring.security.core.properties.BrowserProperties"
},
{
"sourceType": "wyx.practice.spring.security.core.properties.SecurityProperties",
"name": "wyx.security.img-code",
"type": "wyx.practice.spring.security.core.properties.ImageCodeProperties"
}
]
}
我們可以看到properties中已經(jīng)丟失了嵌套類的內(nèi)部屬性,當(dāng)然就無(wú)法正確提示配置信息。
1.1 內(nèi)部類方式生成正確元信息
@ConfigurationProperties(prefix = "wyx.security")
public class SecurityProperties {
private BrowserProperties browser = new BrowserProperties();
private ImageCodeProperties imgCode = new ImageCodeProperties();
public class BrowserProperties {
private String loginPage ;
private LoginType loginType;
}
public class ImageCodeProperties {
private int width=67;
private int height=23;
private int length=4;
private int expireIn=60;
private String urls;
}
//省略get,set方法
}
這么一改我們重新build一次
{
"hints": [],
"groups": [
{
"sourceType": "wyx.practice.spring.security.core.properties.SecurityProperties",
"name": "wyx.security",
"type": "wyx.practice.spring.security.core.properties.SecurityProperties"
},
{
"sourceType": "wyx.practice.spring.security.core.properties.SecurityProperties",
"name": "wyx.security.browser",
"sourceMethod": "getBrowser()",
"type": "wyx.practice.spring.security.core.properties.BrowserProperties"
},
{
"sourceType": "wyx.practice.spring.security.core.properties.SecurityProperties",
"name": "wyx.security.img-code",
"sourceMethod": "getImgCode()",
"type": "wyx.practice.spring.security.core.properties.ImageCodeProperties"
}
],
"properties": [
{
"sourceType": "wyx.practice.spring.security.core.properties.BrowserProperties",
"name": "wyx.security.browser.login-page",
"type": "java.lang.String"
},
{
"sourceType": "wyx.practice.spring.security.core.properties.BrowserProperties",
"name": "wyx.security.browser.login-type",
"type": "wyx.practice.spring.security.core.properties.LoginType"
},
{
"sourceType": "wyx.practice.spring.security.core.properties.ImageCodeProperties",
"name": "wyx.security.img-code.expire-in",
"type": "java.lang.Integer"
},
...
]
}
??果然內(nèi)部屬性可以生成,在編輯時(shí)我們的IDEA也可以提示配置信息了。
??雖然這么做可以解決一部分的情況,實(shí)際開(kāi)發(fā)中我們一些較老的項(xiàng)目已經(jīng)有配置類,如果我們把配置類抽出來(lái)重寫(xiě)成內(nèi)部類顯然是非常麻煩的一種做法,有沒(méi)有一種方式可以直接將外部類引入之后就可以生成正確元信息的方法?
??當(dāng)然是有?。](méi)有我說(shuō)個(gè)JB!
1.2注解方式生成嵌套類元信息
方法非常簡(jiǎn)單,我們只需要在自定義配置類中加入
注:此時(shí)類BrowserProperties ImageCodeProperties已經(jīng)被遷移到外部
@ConfigurationProperties(prefix = "wyx.security")
public class SecurityProperties {
@NestedConfigurationProperty
private BrowserProperties browser = new BrowserProperties();
@NestedConfigurationProperty
private ImageCodeProperties imgCode = new ImageCodeProperties();
}
@NestedConfigurationProperty就是幫助spring-boot-configuration-processor庫(kù)來(lái)識(shí)別并正確生成外部類內(nèi)部屬性的注解。生成的元信息文件我就不貼了。
注:spring-boot-configuration-processor和lombook存在兼容性問(wèn)題,猜測(cè)是因?yàn)橛袝r(shí)lombook生成類get/set方法在spring-boot-configuration-processor之后,導(dǎo)致spring-boot-configuration-processor沒(méi)有獲取到內(nèi)部屬性,在配置類請(qǐng)盡量不要使用lombok注解生成get/set方法.
待續(xù)...
準(zhǔn)備寫(xiě)下list或者map配置信息的獲取