Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
不得不說,Spring為大家提供許多開箱即用的功能,@Value就是一個極其常用的功能,它能將配置信息注入到bean中去。即使是一個簡單的功能,Spring也提供了豐富的注入類型和形式。我經常會忘記一些特別類型注入的寫法,比如說數組,現在整理一下,希望以后不用再找了。
使用@Value注入有三種形式,如下所示:
(1)直接寫值@Value("string value")
這種方式就是直接把要注入的值字面量寫在注解里,比較少用。如果要寫死在注解里了,那直接定義變量的時候寫死就可以了。
(2)占位符@Value("${myvalue}")
這種應該最常用,通過屬性名,將值注入進來。
如果可能為空,需要設置默認值,用法:@Value("${unknown.param:defaultValue}")
(3)SpEL表達式@Value("#{someBean.someValue}")
SpEL表達式很強大,還能在屬性值基礎上加以運算等。
如果可能為空,需要設置默認值,用法:@Value("#{systemProperties['unknown'] ?: 'defaultValue'}")
另外,占位符形式和SpEL表達式是可以結合使用的,如下:
@Value("#{'${listOfValues}'.split(',')}")
private List valuesList;
需要注意的,內外順序不能倒過來,應該要#{}外面,${}在里面。
對于注入的場景,主要有三種:
(1)bean聲明的變量
(2)setter方法注入
(3)構造方法或其它方法的入參
例子代碼如下:
//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;
}
}
//方法入參
public class MyValues {
private String timeZone;
@Autowired
public void configure(@Value("#{systemProperties['user.timezone']}") String timeZone) {
this.timeZone=timeZone;
}
}
既然是注入配置屬性,那就需要有配置文件。對于Springboot,引入配置文件有兩種方法,一種是默認引入的application.properties,另一種則需要通過@PropertySource來引入,引入的方式如下:
@PropertySources({
@PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true),
@PropertySource("classpath:config.properties")
})
public class AppConfig {
//...
}
加上ignoreResourceNotFound后,即使文件找不到,也不會拋FileNotFoundException異常。
現在例舉一些可能使用到的例子,以后在這找就行了。
Java代碼如下所示:
//直接寫值
@Value("plainText")
private String plainText;
//普通形式-字符串
@Value("${myValues.string}")
private String myValuesString;
//普通形式-數字
@Value("${myValues.int}")
private int myValuesInt;
//普通形式-布爾類型
@Value("${myValues.boolean}")
private boolean myValuesBoolean;
//數組
@Value("${myValues.array}")
private String[] myValuesArray;
//Map
@Value("#{${myValues.map}}")
private Map<String, String> myValuesMap;
//操作系統屬性
@Value("#{systemProperties['user.timezone']}")
private String timeZone;
//表達式結果
@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內容為:
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內容為:
上善若水,水利萬物而不爭!
啟動程序,打印以上所有屬性,輸出結果如下所示:
{
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=上善若水,水利萬物而不爭!,
url=<!DOCTYPE html>
<html lang="en">
省略html內容
</html>
}
本文講解了@Value注解的使用,基本上平時開發用到的都涉及了,應該不需要再找其它資料了吧。
歡迎關注公眾號<南瓜慢說>,將持續為你更新...
多讀書,多分享;多寫作,多整理。
TML 中使用 <input> 元素表示單行輸入框和 <textarea> 元素表示多行文本框。
HTML中使用的 <input> 元素在 JavaScript 中對應的是 HTMLInputElement 類型。HTMLInputElement 繼承自 HTMLElement 接口:
interface HTMLInputElement extends HTMLElement {
...
}
HTMLInputElement 類型有一些獨有的屬性和方法:
而在上述介紹 HTMLInputElement 類型中的屬性時,type 屬性要特別關注一下,因為根據 type 屬性的改變,可以改變<input>的屬性。
類型 | 描述 |
text | 文本輸入 |
password | 密碼輸入 |
submit | 表單數據提交 |
button | 按鈕 |
radio | 單選框 |
checkbox | 復選框 |
file | 文件 |
hidden | 隱藏的字段 |
image | 定義圖像作為提交按鈕 |
reset | 重置按鈕 |
省略 type 屬性與 type="text"效果一樣, <input> 元素顯示為文本框。
當 type 的值為text/password/number/時,會有以下屬性對 <input> 元素有效。
屬性 | 類型 | 描述 |
autocomplete | string | 字符串on或off,表示<input>元素的輸入內容可以被瀏覽器自動補全。 |
maxLength | long | 指定<input>元素允許的最多字符數。 |
size | unsigned long | 表示<input>元素的寬度,這個寬度是以字符數來計量的。 |
pattern | string | 表示<input>元素的值應該滿足的正則表達式 |
placeholder | string | 表示<input>元素的占位符,作為對元素的提示。 |
readOnly | boolean | 表示用戶是否可以修改<input>的值。 |
min | string | 表示<input>元素的最小數值或日期。 |
max | string | 表示<input>元素的最大數值或日期。 |
selectionStart | unsigned long | 表示選中文本的起始位置。如果沒有選中文本,返回光標在<input>元素內部的位置。 |
selectionEnd | unsigned long | 表示選中文本的結束位置。如果沒有選中文本,返回光標在<input>元素內部的位置。 |
selectionDirection | string | 表示選中文本的方向。可能的值包括forward、backward、none。 |
下面創建一個 type="text" ,一次顯示 25 個字符,但最多允許顯示 50 個字符的文本框:
<input type="text" size="25" maxlength="50" value="initial value">
HTML 使用的 <textarea> 元素在 JavaScript 中對應的是 HTMLTextAreaElement 類型。HTMLTextAreaElement類型繼承自 HTMLElement 接口:
interface HTMLTextAreaElement extends HTMLElement {
...
}
HTMLTextAreaElement 類型有一些獨有的屬性和方法:
下面創建一個高度為 25,寬度為 5 的 <textarea> 多行文本框。它與 <input> 不同的是,初始值顯示在 <textarea>...</textarea> 之間:
<textarea rows="25" cols="5">initial value</textarea>
注意:處理文本框值的時候最好不要使用 DOM 方法,而應該使用 value 屬性。
<input> 與 <textarea> 都支持 select() 方法,該方法用于選中文本框中的所有內容。該方法的語法為:
select(): void
下面看一個示例:
let textbox=document.forms[0].elements["input-box"];
textbox.select();
也可以在文本框獲得焦點時,選中文本框的內容:
textbox.addEventListener("focus", (event)=> {
event.target.select();
});
當選中文本框中的文本或使用 select() 方法時,會觸發 select 事件。
let textbox=document.forms[0].elements["textbox1"];
textbox.addEventListener("select", (event)=> {
console.log(`Text selected: ${textbox.value}`);
});
HTML5 對 select 事件進行了擴展,通過 selectionStart 和 selectionEnd 屬性獲取文本選區的起點偏移量和終點偏移量。如下所示:
function getSelectedText(textbox){
return textbox.value.substring(textbox.selectionStart,
textbox.selectionEnd);
}
注意:在 IE8 及更早版本不支持這兩個屬性。
HTML5 提供了 setSelectionRange() 方法用于選中部分文本:
setSelectionRange(start, end, direction): void;
下面看一個例子:
<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>
如果想要看到選中效果,必須讓文本框獲得焦點。
不同文本框經常需要保證輸入特定類型或格式的數據,或許數據需要包含特定字符或必須匹配某個特定模式。而文本框并未提供驗證功能,因此要配合 JavaScript 腳本實現輸入過濾功能。
有些輸入框需要出現或不出現特定字符。如果想要將輸入框變成只讀的,只需要使用 preventDefault()方法將按鍵都屏蔽:
input.addEventListener("keypress", (event)=> {
event.preventDefault();
});
而要屏蔽特定字符,就需要檢查事件的 charCode 屬性。如下所示,使用正則表達式實現只允許輸入數字的輸入框:
input.addEventListener("keypress", (event)=> {
if (!/\d/.test(event.key)) {
event.preventDefault();
}
});
還有一個問題需要處理:復制、粘貼及涉及Ctrl 鍵的其他功能。在除IE 外的所有瀏覽器中,前面代碼會屏蔽快捷鍵Ctrl+C、Ctrl+V 及其他使用Ctrl 的組合鍵。因此,最后一項檢測是確保沒有按下Ctrl鍵,如下面的例子所示:
textbox.addEventListener("keypress", (event)=> {
if (!/\d/.test(String.fromCharCode(event.charCode)) &&
event.charCode > 9 &&
!event.ctrlKey){
event.preventDefault();
}
});
最后這個改動可以確保所有默認的文本框行為不受影響。這個技術可以用來自定義是否允許在文本框中輸入某些字符。
IE 是第一個實現了剪切板相關的事件以及通過JavaScript訪問剪切板數據的瀏覽器,其它瀏覽器在后來也都支持了相同的事件和剪切板的訪問,后來 HTML5 將其納入了規范。以下是與剪切板相關的 6 個事件:
剪切板事件的行為及相關對象會因瀏覽器而異。在 Safari、Chrome 和 Firefox 中,beforecopy、beforecut 和 beforepaste 事件只會在顯示文本框的上下文菜單時觸發,但 IE 不僅在這種情況下觸發,也會在 copy、cut 和 paste 事件在所有瀏覽器中都會按預期觸發。
在實際的事件發生之前,通過beforecopy、beforecut 和 beforepaste 事件可以在向剪貼板發送或從中檢索數據前修改數據。不過,取消這些事件并不會取消剪貼板操作。要阻止實際的剪貼板操作,必須取消 copy、cut和 paste 事件。
剪貼板的數據通過 clipboardData 對象來獲取,且clipboardData 對象提供 3 個操作數據的方法:
而 clipboardData 對象在 IE 中使用 window 獲取,在 Firefox、Safari 和 Chrome 中使用 event 獲取。為防止未經授權訪問剪貼板,只能在剪貼板事件期間訪問 clipboardData 對象;IE 會在任何時候都暴露 clipboardData 對象。因此,要兼容兩者,最好在剪貼板事件期間使用該對象。
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);
}
}
如果文本框只有數字,那剪貼時,就需要使用paste事件檢查剪貼板上的文本是否無效。如果無效,可以取消默認行為:
input.addEventListener("paste", (event)=> {
let text=getClipboardText(event);
if (!/^\d*$/.test(text)){
event.preventDefault();
}
});
注意:Firefox、Safari和Chrome只允許在onpaste事件中訪問getData()方法。
在 JavaScript 中,可以用在當前字段完成時自動切換到下一個字段的方式來增強表單字段的易用性。比如,常用手機號分為國家好加手機號。因此,我們設置 2 個文本框:
<form>
<input type="text" name="phone1" id="phone-id-1" maxlength="4">
<input type="text" name="phone2" id="phone-id-2" maxlength="11">
</form>
當文本框輸入到最大允許字符數后,就把焦點移到下一個文本框,這樣可以增加表單的易用性并加速數據輸入。如下所示:
<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() 函數通過比較用戶輸入文本的長度與 maxLength 屬性的值來檢測輸入是否達到了最大長度。如果兩者相等,就通過循環表中的元素集合找到當前文本框,并把焦點設置到下一個元素。
注意:上面的代碼只適用于之前既定的標記,沒有考慮可能存在的隱藏字段。
HTML5 新增了一些表單提交前,瀏覽器會基于指定的規則進行驗證,并在出錯時顯示適當的錯誤信息。而驗證會基于某些條件應用到表單字段中。
表單字段中添加 required 屬性,用于標注該字段是必填項,不填則無法提交。該屬性適用于<input>、<textarea>和<select>。如下所示:
<input type="text" name="account" required>
也可以通過 JavaScript 檢測對應元素的 required 屬性來判斷表單字段是否為必填項:
let isRequired=document.forms[0].elements["account"].required;
也可以檢測瀏覽器是否支持 required 屬性:
let isRequiredSupported="required" in document.createElement("input");
注意:不同瀏覽器處理必填字段的機制不同。Firefox、Chrome、IE 和Opera 會阻止表單提交并在相應字段下面顯示有幫助信息的彈框,而Safari 什么也不做,也不會阻止提交表單。
HTML5 為 <input> 元素增加了幾個新的 type 值。如下所示:
類型 | 描述 |
number | 數字值的輸入 |
date | 日期輸入 |
color | 顏色輸入 |
range | 一定范圍內的值的輸入 |
month | 允許用戶選擇月份和年份 |
week | 允許用戶選擇周和年份 |
time | 允許用戶選擇時間(無時區) |
datetime | 允許用戶選擇日期和時間(有時區) |
datetime-local | 允許用戶選擇日期和時間(無時區) |
電子郵件地址的輸入 | |
search | 搜索(表現類似常規文本) |
tel | 電話號碼的輸入 |
url | URL地址的輸入 |
這些輸入表名字段應該輸入的數據類型,并且提供了默認驗證。如下所示:
<input type="email" name="email">
<input type="url" name="homepage">
要檢測瀏覽器是否支持新類型,可以在 JavaScript 中創建 <input> 并設置 type 屬性,之后讀取它即可。老版本中會將我只類型設置為 text,而支持的會返回正確的值。如下所示:
let input=document.createElement("input");
input.type="email";
let isEmailSupported=(input.type=="email");
而上面介紹的幾個如 number/range/datetime/datetime-local/date/month/week/time 幾個填寫數字的類型,都可以指定 min/max/step 等幾個與數值有關的屬性。step 屬性用于規定合法數字間隔,如 step="2",則合法數字應該為 0、2、4、6,依次類推。如下所示:
<input type="number" min="0" max="100" step="5" name="count">
上面的例子是<input>中只能輸入從 0 到 100 中 5 的倍數。
也可以使用 stepUp() 和 stepDown() 方法對 <input> 元素中的值進行加減,它倆會接收一個可選參數,用于表示加減的數值。如下所示:
input.stepUp(); // 加1
input.stepUp(5); // 加5
input.stepDown(); // 減1
input.stepDown(10); // 減10
HTML5 還為文本添加了 pattern 屬性,用于指定一個正則表達式。這樣就可以自己設置 <input> 元素的輸入模式了。如下所示:
<input type="text" pattern="\d+" name="count">
注意模式的開頭和末尾分別假設有^和$。這意味著輸入內容必須從頭到尾都嚴格與模式匹配。
與新增的輸入類型一樣,指定 pattern 屬性也不會阻止用戶輸入無效內容。模式會應用到值,然后瀏覽器會知道值是否有效。通過訪問 pattern 屬性可以讀取模式:
let pattern=document.forms[0].elements["count"].pattern;
使用如下代碼可以檢測瀏覽器是否支持pattern 屬性:
let isPatternSupported="pattern" in document.createElement("input");
HTML5 新增了 checkValidity() 方法,用來檢測表單中任意給定字段是否有效。而判斷的條件是約束條件,因此必填字段如果沒有值會被視為無效,字段值不匹配 pattern 屬性也會被視為無效。如下所示:
if (document.forms[0].elements[0].checkValidity()){
// 字段有效,繼續
} else {
// 字段無效
}
要檢查整個表單是否有效,可以直接在表單上調用checkValidity()方法。這個方法會在所有字段都有效時返回true,有一個字段無效就會返回false:
if(document.forms[0].checkValidity()){
// 表單有效,繼續
} else {
// 表單無效
}
validity 屬性會返回一個ValidityState 對象,表示 <input> 元素的校驗狀態。返回的對象包含一些列的布爾值的屬性:
因此,通過 validity 屬性可以檢查表單字段的有效性,從而獲取更具體的信息,如下所示:
if (input.validity && !input.validity.valid){
if (input.validity.valueMissing){
console.log("請指定值.")
} else if (input.validity.typeMismatch){
console.log("請指定電子郵件地址.");
} else {
console.log("值無效.");
}
}
通過指定 novalidate 屬性可以禁止對表單進行任何驗證:
<form method="post" action="/signup" novalidate>
<!-- 表單元素 -->
</form>
也可以在 JavaScript 通過 noValidate 屬性設置,為 true 表示屬性存在,為 false 表示屬性不存在:
document.forms[0].noValidate=true; // 關閉驗證
如果一個表單中有多個提交按鈕,那么可以給特定的提交按鈕添加formnovalidate 屬性,指定通過該按鈕無需驗證即可提交表單:
<form method="post" action="/foo">
<!-- 表單元素 -->
<input type="submit" value="注冊提交">
<input type="submit" formnovalidate name="btnNoValidate"
value="沒有驗證的提交按鈕">
</form>
也可以使用 JavaScript 設置 formNoValidate 屬性:
// 關閉驗證
document.forms[0].elements["btnNoValidate"].formNoValidate=true;
以上總結了 <input> 和 <textarea> 兩個元素的一些功能,主要是 <input> 元素可以通過設置 type 屬性獲取不同類型的輸入框,可以通過監聽鍵盤事件并檢測要插入的字符來控制文本框的內容。
還有一些與剪貼板相關的事件,并對剪貼的內容進行檢測。還介紹了一些 HTML5 新增的屬性和方法和新增的更多的 <input> 元素的類型,和一些與驗證相關的屬性和方法。
從一個最簡單的程序開始:
@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 文件內容:
username=coder-xiao-hei
由 AutowiredAnnotationBeanPostProcessor 負責來處理 @Value ,此外該類還負責處理 @Autowired 和 @Inject。
在 AutowiredAnnotationBeanPostProcessor 中有兩個內部類:AutowiredFieldElement 和 AutowiredMethodElement。
當前為 Field 注入,定位到 AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#inject 方法。
通過 debug 可知,整個調用鏈如下:
通過上述的 debug 跟蹤發現可以通過調用 ConfigurableBeanFactory#resolveEmbeddedValue 方法可以獲取占位符的值。
這里的 resolver 是一個 lambda表達式,繼續 debug 我們可以找到具體的執行方法:
到此,我們簡單總結下:
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 是對 profiles 和 properties 的抽象:
現在我們主要來關注 Environment 對 properties 的支持。
下面,我們就來具體看一下 AbstractApplicationContext#finishBeanFactoryInitialization 中的這個 lambda 表達式。
strVal -> getEnvironment().resolvePlaceholders(strVal)
首先,通過 AbstractApplicationContext#getEnvironment 獲取到了 ConfigurableEnvironment 的實例對象,這里創建的其實是 StandardEnvironment 實例對象。
在 StandardEnvironment 中,默認添加了兩個自定義的屬性源,分別是:systemEnvironment 和 systemProperties。
也就是說,@Value 默認是可以注入 system properties 和 system environment 的。
StandardEnvironment 繼承了 AbstractEnvironment 。
在 AbstractEnvironment 中的屬性配置被存放在 MutablePropertySources 中。同時,屬性占位符的數據也來自于此。
MutablePropertySources 中存放了多個 PropertySource ,并且這些 PropertySource 是有順序的。
PropertySource 是 Spring 對配置屬性源的抽象。
name 表示當前屬性源的名稱。source 存放了當前的屬性。
讀者可以自行查看一下最簡單的基于 Map 的實現:MapPropertySource。
有兩種方式可以進行屬性源配置:使用 @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}") // 來自 system properties
private String osName;
@Value("${user.name}") // 通過 MutablePropertySources API 來注冊
private String username;
@Value("${os.version}") // 測試先后順序
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));
// 啟動容器
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 的值都來源于 PropertySource ,而我們可以通過 API 的方式來向 Spring Environment 中添加自定義的 PropertySource。
在此處,我們選擇通過監聽 ApplicationEnvironmentPreparedEvent 事件來實現。
@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() {
// 模擬從配置中心獲取數據
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 文件來注冊:
org.springframework.context.ApplicationListener=com.example.config.CentralConfigPropertySourceListener
一般來說有兩種方案:
Spring 的 @Value 注入是在 Bean 初始化階段執行的。在程序運行過程當中,配置項發生了變更, @Value 并不會重新注入。
我們可以通過增強 @Value 或者自定義新的注解來支持動態更新配置。這里小黑選擇的是第二種方案,自定義新的注解。
@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;
}
/**
* 當配置發生了修改
*
* @param key 配置項
*/
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;
}
}
主測試代碼:
@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);
// 模擬配置發生了更新
TimeUnit.SECONDS.sleep(10);
processor.update("coder.language");
// java222
System.out.println(bean.language);
}
}
作者:Coder小黑
原文鏈接:https://www.cnblogs.com/coderxiaohei/p/14026219.html
*請認真填寫需求信息,我們會在24小時內與您取得聯系。