不得不說(shuō),Spring為大家提供許多開箱即用的功能,@Value就是一個(gè)極其常用的功能,它能將配置信息注入到bean中去。即使是一個(gè)簡(jiǎn)單的功能,Spring也提供了豐富的注入類型和形式。我經(jīng)常會(huì)忘記一些特別類型注入的寫法,比如說(shuō)數(shù)組,現(xiàn)在整理一下,希望以后不用再找了。
使用@Value注入有三種形式,如下所示:
(1)直接寫值@Value("string value")
這種方式就是直接把要注入的值字面量寫在注解里,比較少用。如果要寫死在注解里了,那直接定義變量的時(shí)候?qū)懰谰涂梢粤恕?/p>
(2)占位符@Value("${myvalue}")
這種應(yīng)該最常用,通過屬性名,將值注入進(jìn)來(lái)。
如果可能為空,需要設(shè)置默認(rèn)值,用法:@Value("${unknown.param:defaultValue}")
(3)SpEL表達(dá)式@Value("#{someBean.someValue}")
SpEL表達(dá)式很強(qiáng)大,還能在屬性值基礎(chǔ)上加以運(yùn)算等。
如果可能為空,需要設(shè)置默認(rèn)值,用法:@Value("#{systemProperties['unknown'] ?: 'defaultValue'}")
另外,占位符形式和SpEL表達(dá)式是可以結(jié)合使用的,如下:
@Value("#{'${listOfValues}'.split(',')}")
private List valuesList;
需要注意的,內(nèi)外順序不能倒過來(lái),應(yīng)該要#{}外面,${}在里面。
對(duì)于注入的場(chǎng)景,主要有三種:
(1)bean聲明的變量
(2)setter方法注入
(3)構(gòu)造方法或其它方法的入?yún)?/p>
例子代碼如下:
//bean聲明的變量
public static class MyValues {
@Value("#{systemProperties['user.timezone']}")
private String timeZone;
}
//setter 方法中
public static class MyValues {
private String timeZone;
@Value("#{systemProperties['user.timezone']}")
public void setTimeZone(String timeZone) {
this.timeZone=timeZone;
}
}
//方法入?yún)?
public class MyValues {
private String timeZone;
@Autowired
public void configure(@Value("#{systemProperties['user.timezone']}") String timeZone) {
this.timeZone=timeZone;
}
}
既然是注入配置屬性,那就需要有配置文件。對(duì)于Springboot,引入配置文件有兩種方法,一種是默認(rèn)引入的application.properties,另一種則需要通過@PropertySource來(lái)引入,引入的方式如下:
@PropertySources({
@PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true),
@PropertySource("classpath:config.properties")
})
public class AppConfig {
//...
}
加上ignoreResourceNotFound后,即使文件找不到,也不會(huì)拋FileNotFoundException異常。
現(xiàn)在例舉一些可能使用到的例子,以后在這找就行了。
Java代碼如下所示:
//直接寫值
@Value("plainText")
private String plainText;
//普通形式-字符串
@Value("${myValues.string}")
private String myValuesString;
//普通形式-數(shù)字
@Value("${myValues.int}")
private int myValuesInt;
//普通形式-布爾類型
@Value("${myValues.boolean}")
private boolean myValuesBoolean;
//數(shù)組
@Value("${myValues.array}")
private String[] myValuesArray;
//Map
@Value("#{${myValues.map}}")
private Map<String, String> myValuesMap;
//操作系統(tǒng)屬性
@Value("#{systemProperties['user.timezone']}")
private String timeZone;
//表達(dá)式結(jié)果
@Value("#{ T(java.lang.Math).random() * 100.0 }")
private double randomNumber;
//其它bean的屬性
@Value("#{propertiesApplication.class.getName()}")
private String className;
//文件資源
@Value("classpath:larry.txt")
private Resource file;
//URL資源
@Value("https://www.github.com")
private Resource url;
其中,配置文件application.properties內(nèi)容為:
myValues.int=99
myValues.boolean=true
myValues.string=Larry
myValues.array=my,name,is,larry
myValues.map={name: 'Larry', age: '18', city: 'Guangzhou'}
資源文件larry.txt內(nèi)容為:
上善若水,水利萬(wàn)物而不爭(zhēng)!
啟動(dòng)程序,打印以上所有屬性,輸出結(jié)果如下所示:
{
plainText='plainText',
myValuesString='Larry',
myValuesInt=99,
myValuesBoolean=true,
myValuesArray=[my, name, is, larry],
myValuesMap={name=Larry, age=18, city=Guangzhou},
timeZone='Asia/Shanghai',
randomNumber=19.775129662772294,
className='com.pkslow.properties.PropertiesApplication$$EnhancerBySpringCGLIB$$4d0912c',
file=上善若水,水利萬(wàn)物而不爭(zhēng)!,
url=<!DOCTYPE html>
<html lang="en">
省略html內(nèi)容
</html>
}
本文講解了@Value注解的使用,基本上平時(shí)開發(fā)用到的都涉及了,應(yīng)該不需要再找其它資料了吧。
歡迎關(guān)注公眾號(hào)<南瓜慢說(shuō)>,將持續(xù)為你更新...
多讀書,多分享;多寫作,多整理。
TML 中使用 <input> 元素表示單行輸入框和 <textarea> 元素表示多行文本框。
HTML中使用的 <input> 元素在 JavaScript 中對(duì)應(yīng)的是 HTMLInputElement 類型。HTMLInputElement 繼承自 HTMLElement 接口:
interface HTMLInputElement extends HTMLElement {
...
}
HTMLInputElement 類型有一些獨(dú)有的屬性和方法:
而在上述介紹 HTMLInputElement 類型中的屬性時(shí),type 屬性要特別關(guān)注一下,因?yàn)楦鶕?jù) type 屬性的改變,可以改變<input>的屬性。
類型 | 描述 |
text | 文本輸入 |
password | 密碼輸入 |
submit | 表單數(shù)據(jù)提交 |
button | 按鈕 |
radio | 單選框 |
checkbox | 復(fù)選框 |
file | 文件 |
hidden | 隱藏的字段 |
image | 定義圖像作為提交按鈕 |
reset | 重置按鈕 |
省略 type 屬性與 type="text"效果一樣, <input> 元素顯示為文本框。
當(dāng) type 的值為text/password/number/時(shí),會(huì)有以下屬性對(duì) <input> 元素有效。
屬性 | 類型 | 描述 |
autocomplete | string | 字符串on或off,表示<input>元素的輸入內(nèi)容可以被瀏覽器自動(dòng)補(bǔ)全。 |
maxLength | long | 指定<input>元素允許的最多字符數(shù)。 |
size | unsigned long | 表示<input>元素的寬度,這個(gè)寬度是以字符數(shù)來(lái)計(jì)量的。 |
pattern | string | 表示<input>元素的值應(yīng)該滿足的正則表達(dá)式 |
placeholder | string | 表示<input>元素的占位符,作為對(duì)元素的提示。 |
readOnly | boolean | 表示用戶是否可以修改<input>的值。 |
min | string | 表示<input>元素的最小數(shù)值或日期。 |
max | string | 表示<input>元素的最大數(shù)值或日期。 |
selectionStart | unsigned long | 表示選中文本的起始位置。如果沒有選中文本,返回光標(biāo)在<input>元素內(nèi)部的位置。 |
selectionEnd | unsigned long | 表示選中文本的結(jié)束位置。如果沒有選中文本,返回光標(biāo)在<input>元素內(nèi)部的位置。 |
selectionDirection | string | 表示選中文本的方向。可能的值包括forward、backward、none。 |
下面創(chuàng)建一個(gè) type="text" ,一次顯示 25 個(gè)字符,但最多允許顯示 50 個(gè)字符的文本框:
<input type="text" size="25" maxlength="50" value="initial value">
HTML 使用的 <textarea> 元素在 JavaScript 中對(duì)應(yīng)的是 HTMLTextAreaElement 類型。HTMLTextAreaElement類型繼承自 HTMLElement 接口:
interface HTMLTextAreaElement extends HTMLElement {
...
}
HTMLTextAreaElement 類型有一些獨(dú)有的屬性和方法:
下面創(chuàng)建一個(gè)高度為 25,寬度為 5 的 <textarea> 多行文本框。它與 <input> 不同的是,初始值顯示在 <textarea>...</textarea> 之間:
<textarea rows="25" cols="5">initial value</textarea>
注意:處理文本框值的時(shí)候最好不要使用 DOM 方法,而應(yīng)該使用 value 屬性。
<input> 與 <textarea> 都支持 select() 方法,該方法用于選中文本框中的所有內(nèi)容。該方法的語(yǔ)法為:
select(): void
下面看一個(gè)示例:
let textbox=document.forms[0].elements["input-box"];
textbox.select();
也可以在文本框獲得焦點(diǎn)時(shí),選中文本框的內(nèi)容:
textbox.addEventListener("focus", (event)=> {
event.target.select();
});
當(dāng)選中文本框中的文本或使用 select() 方法時(shí),會(huì)觸發(fā) select 事件。
let textbox=document.forms[0].elements["textbox1"];
textbox.addEventListener("select", (event)=> {
console.log(`Text selected: ${textbox.value}`);
});
HTML5 對(duì) select 事件進(jìn)行了擴(kuò)展,通過 selectionStart 和 selectionEnd 屬性獲取文本選區(qū)的起點(diǎn)偏移量和終點(diǎn)偏移量。如下所示:
function getSelectedText(textbox){
return textbox.value.substring(textbox.selectionStart,
textbox.selectionEnd);
}
注意:在 IE8 及更早版本不支持這兩個(gè)屬性。
HTML5 提供了 setSelectionRange() 方法用于選中部分文本:
setSelectionRange(start, end, direction): void;
下面看一個(gè)例子:
<input type="text" id="text-sample" size="20" value="Hello World!">
<button onclick="selectText()">選中部分文本</button>
<script>
function selectText() {
let input=document.getElementById("text-sample");
input.focus();
input.setSelectionRange(4, 8); // o Wo
}
</script>
如果想要看到選中效果,必須讓文本框獲得焦點(diǎn)。
不同文本框經(jīng)常需要保證輸入特定類型或格式的數(shù)據(jù),或許數(shù)據(jù)需要包含特定字符或必須匹配某個(gè)特定模式。而文本框并未提供驗(yàn)證功能,因此要配合 JavaScript 腳本實(shí)現(xiàn)輸入過濾功能。
有些輸入框需要出現(xiàn)或不出現(xiàn)特定字符。如果想要將輸入框變成只讀的,只需要使用 preventDefault()方法將按鍵都屏蔽:
input.addEventListener("keypress", (event)=> {
event.preventDefault();
});
而要屏蔽特定字符,就需要檢查事件的 charCode 屬性。如下所示,使用正則表達(dá)式實(shí)現(xiàn)只允許輸入數(shù)字的輸入框:
input.addEventListener("keypress", (event)=> {
if (!/\d/.test(event.key)) {
event.preventDefault();
}
});
還有一個(gè)問題需要處理:復(fù)制、粘貼及涉及Ctrl 鍵的其他功能。在除IE 外的所有瀏覽器中,前面代碼會(huì)屏蔽快捷鍵Ctrl+C、Ctrl+V 及其他使用Ctrl 的組合鍵。因此,最后一項(xiàng)檢測(cè)是確保沒有按下Ctrl鍵,如下面的例子所示:
textbox.addEventListener("keypress", (event)=> {
if (!/\d/.test(String.fromCharCode(event.charCode)) &&
event.charCode > 9 &&
!event.ctrlKey){
event.preventDefault();
}
});
最后這個(gè)改動(dòng)可以確保所有默認(rèn)的文本框行為不受影響。這個(gè)技術(shù)可以用來(lái)自定義是否允許在文本框中輸入某些字符。
IE 是第一個(gè)實(shí)現(xiàn)了剪切板相關(guān)的事件以及通過JavaScript訪問剪切板數(shù)據(jù)的瀏覽器,其它瀏覽器在后來(lái)也都支持了相同的事件和剪切板的訪問,后來(lái) HTML5 將其納入了規(guī)范。以下是與剪切板相關(guān)的 6 個(gè)事件:
剪切板事件的行為及相關(guān)對(duì)象會(huì)因?yàn)g覽器而異。在 Safari、Chrome 和 Firefox 中,beforecopy、beforecut 和 beforepaste 事件只會(huì)在顯示文本框的上下文菜單時(shí)觸發(fā),但 IE 不僅在這種情況下觸發(fā),也會(huì)在 copy、cut 和 paste 事件在所有瀏覽器中都會(huì)按預(yù)期觸發(fā)。
在實(shí)際的事件發(fā)生之前,通過beforecopy、beforecut 和 beforepaste 事件可以在向剪貼板發(fā)送或從中檢索數(shù)據(jù)前修改數(shù)據(jù)。不過,取消這些事件并不會(huì)取消剪貼板操作。要阻止實(shí)際的剪貼板操作,必須取消 copy、cut和 paste 事件。
剪貼板的數(shù)據(jù)通過 clipboardData 對(duì)象來(lái)獲取,且clipboardData 對(duì)象提供 3 個(gè)操作數(shù)據(jù)的方法:
而 clipboardData 對(duì)象在 IE 中使用 window 獲取,在 Firefox、Safari 和 Chrome 中使用 event 獲取。為防止未經(jīng)授權(quán)訪問剪貼板,只能在剪貼板事件期間訪問 clipboardData 對(duì)象;IE 會(huì)在任何時(shí)候都暴露 clipboardData 對(duì)象。因此,要兼容兩者,最好在剪貼板事件期間使用該對(duì)象。
function getClipboardText(event){
var clipboardData=(event.clipboardData || window.clipboardData);
return clipboardData.getData("text");
}
function setClipboardText (event, value){
if (event.clipboardData){
return event.clipboardData.setData("text/plain", value);
} else if (window.clipboardData){
return window.clipboardData.setData("text", value);
}
}
如果文本框只有數(shù)字,那剪貼時(shí),就需要使用paste事件檢查剪貼板上的文本是否無(wú)效。如果無(wú)效,可以取消默認(rèn)行為:
input.addEventListener("paste", (event)=> {
let text=getClipboardText(event);
if (!/^\d*$/.test(text)){
event.preventDefault();
}
});
注意:Firefox、Safari和Chrome只允許在onpaste事件中訪問getData()方法。
在 JavaScript 中,可以用在當(dāng)前字段完成時(shí)自動(dòng)切換到下一個(gè)字段的方式來(lái)增強(qiáng)表單字段的易用性。比如,常用手機(jī)號(hào)分為國(guó)家好加手機(jī)號(hào)。因此,我們?cè)O(shè)置 2 個(gè)文本框:
<form>
<input type="text" name="phone1" id="phone-id-1" maxlength="4">
<input type="text" name="phone2" id="phone-id-2" maxlength="11">
</form>
當(dāng)文本框輸入到最大允許字符數(shù)后,就把焦點(diǎn)移到下一個(gè)文本框,這樣可以增加表單的易用性并加速數(shù)據(jù)輸入。如下所示:
<script>
function tabForward(event){
let target=event.target;
if (target.value.length==target.maxLength){
let form=target.form;
for (let i=0, len=form.elements.length; i < len; i++) {
if (form.elements[i]==target) {
if (form.elements[i+1]) {
form.elements[i+1].focus();
}
return;
}
}
}
}
let inputIds=["phone-id-1", "phone-id-2"];
for (let id of inputIds) {
let textbox=document.getElementById(id);
textbox.addEventListener("keyup", tabForward);
}
</script>
這里,tabForward() 函數(shù)通過比較用戶輸入文本的長(zhǎng)度與 maxLength 屬性的值來(lái)檢測(cè)輸入是否達(dá)到了最大長(zhǎng)度。如果兩者相等,就通過循環(huán)表中的元素集合找到當(dāng)前文本框,并把焦點(diǎn)設(shè)置到下一個(gè)元素。
注意:上面的代碼只適用于之前既定的標(biāo)記,沒有考慮可能存在的隱藏字段。
HTML5 新增了一些表單提交前,瀏覽器會(huì)基于指定的規(guī)則進(jìn)行驗(yàn)證,并在出錯(cuò)時(shí)顯示適當(dāng)?shù)腻e(cuò)誤信息。而驗(yàn)證會(huì)基于某些條件應(yīng)用到表單字段中。
表單字段中添加 required 屬性,用于標(biāo)注該字段是必填項(xiàng),不填則無(wú)法提交。該屬性適用于<input>、<textarea>和<select>。如下所示:
<input type="text" name="account" required>
也可以通過 JavaScript 檢測(cè)對(duì)應(yīng)元素的 required 屬性來(lái)判斷表單字段是否為必填項(xiàng):
let isRequired=document.forms[0].elements["account"].required;
也可以檢測(cè)瀏覽器是否支持 required 屬性:
let isRequiredSupported="required" in document.createElement("input");
注意:不同瀏覽器處理必填字段的機(jī)制不同。Firefox、Chrome、IE 和Opera 會(huì)阻止表單提交并在相應(yīng)字段下面顯示有幫助信息的彈框,而Safari 什么也不做,也不會(huì)阻止提交表單。
HTML5 為 <input> 元素增加了幾個(gè)新的 type 值。如下所示:
類型 | 描述 |
number | 數(shù)字值的輸入 |
date | 日期輸入 |
color | 顏色輸入 |
range | 一定范圍內(nèi)的值的輸入 |
month | 允許用戶選擇月份和年份 |
week | 允許用戶選擇周和年份 |
time | 允許用戶選擇時(shí)間(無(wú)時(shí)區(qū)) |
datetime | 允許用戶選擇日期和時(shí)間(有時(shí)區(qū)) |
datetime-local | 允許用戶選擇日期和時(shí)間(無(wú)時(shí)區(qū)) |
電子郵件地址的輸入 | |
search | 搜索(表現(xiàn)類似常規(guī)文本) |
tel | 電話號(hào)碼的輸入 |
url | URL地址的輸入 |
這些輸入表名字段應(yīng)該輸入的數(shù)據(jù)類型,并且提供了默認(rèn)驗(yàn)證。如下所示:
<input type="email" name="email">
<input type="url" name="homepage">
要檢測(cè)瀏覽器是否支持新類型,可以在 JavaScript 中創(chuàng)建 <input> 并設(shè)置 type 屬性,之后讀取它即可。老版本中會(huì)將我只類型設(shè)置為 text,而支持的會(huì)返回正確的值。如下所示:
let input=document.createElement("input");
input.type="email";
let isEmailSupported=(input.type=="email");
而上面介紹的幾個(gè)如 number/range/datetime/datetime-local/date/month/week/time 幾個(gè)填寫數(shù)字的類型,都可以指定 min/max/step 等幾個(gè)與數(shù)值有關(guān)的屬性。step 屬性用于規(guī)定合法數(shù)字間隔,如 step="2",則合法數(shù)字應(yīng)該為 0、2、4、6,依次類推。如下所示:
<input type="number" min="0" max="100" step="5" name="count">
上面的例子是<input>中只能輸入從 0 到 100 中 5 的倍數(shù)。
也可以使用 stepUp() 和 stepDown() 方法對(duì) <input> 元素中的值進(jìn)行加減,它倆會(huì)接收一個(gè)可選參數(shù),用于表示加減的數(shù)值。如下所示:
input.stepUp(); // 加1
input.stepUp(5); // 加5
input.stepDown(); // 減1
input.stepDown(10); // 減10
HTML5 還為文本添加了 pattern 屬性,用于指定一個(gè)正則表達(dá)式。這樣就可以自己設(shè)置 <input> 元素的輸入模式了。如下所示:
<input type="text" pattern="\d+" name="count">
注意模式的開頭和末尾分別假設(shè)有^和$。這意味著輸入內(nèi)容必須從頭到尾都嚴(yán)格與模式匹配。
與新增的輸入類型一樣,指定 pattern 屬性也不會(huì)阻止用戶輸入無(wú)效內(nèi)容。模式會(huì)應(yīng)用到值,然后瀏覽器會(huì)知道值是否有效。通過訪問 pattern 屬性可以讀取模式:
let pattern=document.forms[0].elements["count"].pattern;
使用如下代碼可以檢測(cè)瀏覽器是否支持pattern 屬性:
let isPatternSupported="pattern" in document.createElement("input");
HTML5 新增了 checkValidity() 方法,用來(lái)檢測(cè)表單中任意給定字段是否有效。而判斷的條件是約束條件,因此必填字段如果沒有值會(huì)被視為無(wú)效,字段值不匹配 pattern 屬性也會(huì)被視為無(wú)效。如下所示:
if (document.forms[0].elements[0].checkValidity()){
// 字段有效,繼續(xù)
} else {
// 字段無(wú)效
}
要檢查整個(gè)表單是否有效,可以直接在表單上調(diào)用checkValidity()方法。這個(gè)方法會(huì)在所有字段都有效時(shí)返回true,有一個(gè)字段無(wú)效就會(huì)返回false:
if(document.forms[0].checkValidity()){
// 表單有效,繼續(xù)
} else {
// 表單無(wú)效
}
validity 屬性會(huì)返回一個(gè)ValidityState 對(duì)象,表示 <input> 元素的校驗(yàn)狀態(tài)。返回的對(duì)象包含一些列的布爾值的屬性:
因此,通過 validity 屬性可以檢查表單字段的有效性,從而獲取更具體的信息,如下所示:
if (input.validity && !input.validity.valid){
if (input.validity.valueMissing){
console.log("請(qǐng)指定值.")
} else if (input.validity.typeMismatch){
console.log("請(qǐng)指定電子郵件地址.");
} else {
console.log("值無(wú)效.");
}
}
通過指定 novalidate 屬性可以禁止對(duì)表單進(jìn)行任何驗(yàn)證:
<form method="post" action="/signup" novalidate>
<!-- 表單元素 -->
</form>
也可以在 JavaScript 通過 noValidate 屬性設(shè)置,為 true 表示屬性存在,為 false 表示屬性不存在:
document.forms[0].noValidate=true; // 關(guān)閉驗(yàn)證
如果一個(gè)表單中有多個(gè)提交按鈕,那么可以給特定的提交按鈕添加formnovalidate 屬性,指定通過該按鈕無(wú)需驗(yàn)證即可提交表單:
<form method="post" action="/foo">
<!-- 表單元素 -->
<input type="submit" value="注冊(cè)提交">
<input type="submit" formnovalidate name="btnNoValidate"
value="沒有驗(yàn)證的提交按鈕">
</form>
也可以使用 JavaScript 設(shè)置 formNoValidate 屬性:
// 關(guān)閉驗(yàn)證
document.forms[0].elements["btnNoValidate"].formNoValidate=true;
以上總結(jié)了 <input> 和 <textarea> 兩個(gè)元素的一些功能,主要是 <input> 元素可以通過設(shè)置 type 屬性獲取不同類型的輸入框,可以通過監(jiān)聽鍵盤事件并檢測(cè)要插入的字符來(lái)控制文本框的內(nèi)容。
還有一些與剪貼板相關(guān)的事件,并對(duì)剪貼的內(nèi)容進(jìn)行檢測(cè)。還介紹了一些 HTML5 新增的屬性和方法和新增的更多的 <input> 元素的類型,和一些與驗(yàn)證相關(guān)的屬性和方法。
從一個(gè)最簡(jiǎn)單的程序開始:
@Configuration
@PropertySource("classpath:application.properties")
public class ValueAnnotationDemo {
@Value("${username}")
private String username;
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(ValueAnnotationDemo.class);
System.out.println(context.getBean(ValueAnnotationDemo.class).username);
context.close();
}
}
application.properties 文件內(nèi)容:
username=coder-xiao-hei
由 AutowiredAnnotationBeanPostProcessor 負(fù)責(zé)來(lái)處理 @Value ,此外該類還負(fù)責(zé)處理 @Autowired 和 @Inject。
在 AutowiredAnnotationBeanPostProcessor 中有兩個(gè)內(nèi)部類:AutowiredFieldElement 和 AutowiredMethodElement。
當(dāng)前為 Field 注入,定位到 AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#inject 方法。
通過 debug 可知,整個(gè)調(diào)用鏈如下:
通過上述的 debug 跟蹤發(fā)現(xiàn)可以通過調(diào)用 ConfigurableBeanFactory#resolveEmbeddedValue 方法可以獲取占位符的值。
這里的 resolver 是一個(gè) lambda表達(dá)式,繼續(xù) debug 我們可以找到具體的執(zhí)行方法:
到此,我們簡(jiǎn)單總結(jié)下:
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-environment
The Environment interface is an abstraction integrated in the container that models two key aspects of the application environment: profiles and properties.
A profile is a named, logical group of bean definitions to be registered with the container only if the given profile is active. Beans may be assigned to a profile whether defined in XML or with annotations. The role of the Environment object with relation to profiles is in determining which profiles (if any) are currently active, and which profiles (if any) should be active by default.
Properties play an important role in almost all applications and may originate from a variety of sources: properties files, JVM system properties, system environment variables, JNDI, servlet context parameters, ad-hoc Properties objects, Map objects, and so on. The role of the Environment object with relation to properties is to provide the user with a convenient service interface for configuring property sources and resolving properties from them.
Environment 是對(duì) profiles 和 properties 的抽象:
現(xiàn)在我們主要來(lái)關(guān)注 Environment 對(duì) properties 的支持。
下面,我們就來(lái)具體看一下 AbstractApplicationContext#finishBeanFactoryInitialization 中的這個(gè) lambda 表達(dá)式。
strVal -> getEnvironment().resolvePlaceholders(strVal)
首先,通過 AbstractApplicationContext#getEnvironment 獲取到了 ConfigurableEnvironment 的實(shí)例對(duì)象,這里創(chuàng)建的其實(shí)是 StandardEnvironment 實(shí)例對(duì)象。
在 StandardEnvironment 中,默認(rèn)添加了兩個(gè)自定義的屬性源,分別是:systemEnvironment 和 systemProperties。
也就是說(shuō),@Value 默認(rèn)是可以注入 system properties 和 system environment 的。
StandardEnvironment 繼承了 AbstractEnvironment 。
在 AbstractEnvironment 中的屬性配置被存放在 MutablePropertySources 中。同時(shí),屬性占位符的數(shù)據(jù)也來(lái)自于此。
MutablePropertySources 中存放了多個(gè) PropertySource ,并且這些 PropertySource 是有順序的。
PropertySource 是 Spring 對(duì)配置屬性源的抽象。
name 表示當(dāng)前屬性源的名稱。source 存放了當(dāng)前的屬性。
讀者可以自行查看一下最簡(jiǎn)單的基于 Map 的實(shí)現(xiàn):MapPropertySource。
有兩種方式可以進(jìn)行屬性源配置:使用 @PropertySource 注解,或者通過 MutablePropertySources 的 API。例如:
@Configuration
@PropertySource("classpath:application.properties")
public class ValueAnnotationDemo {
@Value("${username}")
private String username;
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(ValueAnnotationDemo.class);
Map<String, Object> map=new HashMap<>();
map.put("my.name", "coder小黑");
context.getEnvironment()
.getPropertySources()
.addFirst(new MapPropertySource("coder-xiaohei-test", map));
}
}
public class Demo {
@Value("${os.name}") // 來(lái)自 system properties
private String osName;
@Value("${user.name}") // 通過 MutablePropertySources API 來(lái)注冊(cè)
private String username;
@Value("${os.version}") // 測(cè)試先后順序
private String osVersion;
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.register(Demo.class);
ConfigurableEnvironment environment=context.getEnvironment();
MutablePropertySources propertySources=environment.getPropertySources();
Map<String, Object> source=new HashMap<>();
source.put("user.name", "xiaohei");
source.put("os.version", "version-for-xiaohei");
// 添加自定義 MapPropertySource,且放在第一位
propertySources.addFirst(new MapPropertySource("coder-xiao-hei-test", source));
// 啟動(dòng)容器
context.refresh();
Demo bean=context.getBean(Demo.class);
// Mac OS X
System.out.println(bean.osName);
// xiaohei
System.out.println(bean.username);
// version-for-xiaohei
System.out.println(bean.osVersion);
// Mac OS X
System.out.println(System.getProperty("os.name"));
// 10.15.7
System.out.println(System.getProperty("os.version"));
// xiaohei
System.out.println(environment.getProperty("user.name"));
//xiaohei
System.out.println(environment.resolvePlaceholders("${user.name}"));
context.close();
}
}
@Value 的值都來(lái)源于 PropertySource ,而我們可以通過 API 的方式來(lái)向 Spring Environment 中添加自定義的 PropertySource。
在此處,我們選擇通過監(jiān)聽 ApplicationEnvironmentPreparedEvent 事件來(lái)實(shí)現(xiàn)。
@Slf4j
public class CentralConfigPropertySourceListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private final CentralConfig centralConfig=new CentralConfig();
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
centralConfig.loadCentralConfig();
event.getEnvironment().getPropertySources().addFirst(new CentralConfigPropertySource(centralConfig));
}
static class CentralConfig {
private volatile Map<String, Object> config=new HashMap<>();
private void loadCentralConfig() {
// 模擬從配置中心獲取數(shù)據(jù)
config.put("coder.name", "xiaohei");
config.put("coder.language", "java");
new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 模擬配置更新
config.put("coder.language", "java222");
System.out.println("update 'coder.language' success");
}).start();
}
}
static class CentralConfigPropertySource extends EnumerablePropertySource<CentralConfig> {
private static final String PROPERTY_SOURCE_NAME="centralConfigPropertySource";
public CentralConfigPropertySource(CentralConfig source) {
super(PROPERTY_SOURCE_NAME, source);
}
@Override
@Nullable
public Object getProperty(String name) {
return this.source.config.get(name);
}
@Override
public boolean containsProperty(String name) {
return this.source.config.containsKey(name);
}
@Override
public String[] getPropertyNames() {
return StringUtils.toStringArray(this.source.config.keySet());
}
}
}
通過 META-INF/spring.factories 文件來(lái)注冊(cè):
org.springframework.context.ApplicationListener=com.example.config.CentralConfigPropertySourceListener
一般來(lái)說(shuō)有兩種方案:
Spring 的 @Value 注入是在 Bean 初始化階段執(zhí)行的。在程序運(yùn)行過程當(dāng)中,配置項(xiàng)發(fā)生了變更, @Value 并不會(huì)重新注入。
我們可以通過增強(qiáng) @Value 或者自定義新的注解來(lái)支持動(dòng)態(tài)更新配置。這里小黑選擇的是第二種方案,自定義新的注解。
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigValue {
String value();
}
@Component
public class ConfigValueAnnotationBeanPostProcessor implements BeanPostProcessor, EnvironmentAware {
private static final PropertyPlaceholderHelper PROPERTY_PLACEHOLDER_HELPER= new PropertyPlaceholderHelper(
SystemPropertyUtils.PLACEHOLDER_PREFIX,
SystemPropertyUtils.PLACEHOLDER_SUFFIX,
SystemPropertyUtils.VALUE_SEPARATOR,
false);
private MultiValueMap<String, ConfigValueHolder> keyHolder=new LinkedMultiValueMap<>();
private Environment environment;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
ReflectionUtils.doWithFields(bean.getClass(),
field -> {
ConfigValue annotation=AnnotationUtils.findAnnotation(field, ConfigValue.class);
if (annotation==null) {
return;
}
String value=environment.resolvePlaceholders(annotation.value());
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, bean, value);
String key=PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders(annotation.value(), placeholderName -> placeholderName);
ConfigValueHolder configValueHolder=new ConfigValueHolder(bean, beanName, field, key);
keyHolder.add(key, configValueHolder);
});
return bean;
}
/**
* 當(dāng)配置發(fā)生了修改
*
* @param key 配置項(xiàng)
*/
public void update(String key) {
List<ConfigValueHolder> configValueHolders=keyHolder.get(key);
if (CollectionUtils.isEmpty(configValueHolders)) {
return;
}
String property=environment.getProperty(key);
configValueHolders.forEach(holder -> ReflectionUtils.setField(holder.field, holder.bean, property));
}
@Override
public void setEnvironment(Environment environment) {
this.environment=environment;
}
@AllArgsConstructor
static class ConfigValueHolder {
final Object bean;
final String beanName;
final Field field;
final String key;
}
}
主測(cè)試代碼:
@SpringBootApplication
public class ConfigApplication {
@Value("${coder.name}")
String coderName;
@ConfigValue("${coder.language}")
String language;
public static void main(String[] args) throws InterruptedException {
ConfigurableApplicationContext context=SpringApplication.run(ConfigApplication.class, args);
ConfigApplication bean=context.getBean(ConfigApplication.class);
// xiaohei
System.out.println(bean.coderName);
// java
System.out.println(bean.language);
ConfigValueAnnotationBeanPostProcessor processor=context.getBean(ConfigValueAnnotationBeanPostProcessor.class);
// 模擬配置發(fā)生了更新
TimeUnit.SECONDS.sleep(10);
processor.update("coder.language");
// java222
System.out.println(bean.language);
}
}
作者:Coder小黑
原文鏈接:https://www.cnblogs.com/coderxiaohei/p/14026219.html
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。