Springboot內建ApplicationListener--FileEncodingApplicationListener
阿新 • • 發佈:2018-11-19
原始碼分析
本文原始碼基於 Springboot 2.1.0
package org.springframework.boot.context;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
/**
* An link ApplicationListener that halts application startup if the system file
* encoding does not match an expected value set in the environment. By default has no
* effect, but if you set spring.mandatory_file_encoding (or some camelCase or
* UPPERCASE variant of that) to the name of a character encoding (e.g. "UTF-8") then this
* initializer throws an exception when the file.encoding System property does not
* equal it.
*
* 用於在系統檔案編碼字符集和環境變數中設定的期望值不一樣時終止應用程式的啟動,通過拋
* IllegalStateException異常的方式。確證情況下這個ApplicationListener並不起什麼作用,
* 因為你的環境中預設不指定spring.mandatory_file_encoding,但是如果你設定了這個環境屬性
* 並且它的值跟系統屬性file.encoding不一樣,該監聽器就會在應用啟動過程中丟擲一個異常從而
* 終止應用程式的啟動。
*
* 關注事件 : ApplicationEnvironmentPreparedEvent
*
* The System property file.encoding is normally set by the JVM in response to the
* LANG or LC_ALL environment variables. It is used (along with other
* platform-dependent variables keyed off those environment variables) to encode JVM
* arguments as well as file names and paths. In most cases you can override the file
* encoding System property on the command line (with standard JVM features), but also
* consider setting the LANG environment variable to an explicit
* character-encoding value (e.g. "en_GB.UTF-8").
*
* 系統屬性file.encoding通常由JVM根據環境變數LANG或者LC_ALL來推斷和設定。它被用於編碼JVM
* 引數,檔名稱,檔案系統路徑字串。在大多數情況下,你可以在JVM命令列中重寫系統屬性file.encoding,
* 但是也可以考慮將系統環境變數LANG明確地直接設定成所需要的編碼字符集從而避免指定file.encoding引數。
*
* @author Dave Syer
* @author Madhura Bhave
*/
public class FileEncodingApplicationListener
implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
private static final Log logger = LogFactory
.getLog(FileEncodingApplicationListener.class);
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
if (!environment.containsProperty("spring.mandatory-file-encoding")) {
// 如果環境屬性中不包含屬性 spring.mandatory-file-encoding,則什麼都不做
// 這也是預設情況下該事件監聽器看起來對應用啟動沒有任何影響的原因:
// 它僅在環境屬性 spring.mandatory-file-encoding 設定時在執行給自己設定的
// 檢查邏輯;
return;
}
String encoding = System.getProperty("file.encoding");
String desired = environment.getProperty("spring.mandatory-file-encoding");
// 如果系統屬性 file.encoding 和環境屬性 spring.mandatory-file-encoding 不同,
// 則日誌輸出該錯誤資訊並且丟擲異常IllegalStateException終止應用程式的啟動。
// 注意這裡的相等比較方式 : 大小寫不敏感
if (encoding != null && !desired.equalsIgnoreCase(encoding)) {
logger.error("System property 'file.encoding' is currently '" + encoding
+ "'. It should be '" + desired
+ "' (as defined in 'spring.mandatoryFileEncoding').");
logger.error("Environment variable LANG is '" + System.getenv("LANG")
+ "'. You could use a locale setting that matches encoding='"
+ desired + "'.");
logger.error("Environment variable LC_ALL is '" + System.getenv("LC_ALL")
+ "'. You could use a locale setting that matches encoding='"
+ desired + "'.");
throw new IllegalStateException(
"The Java Virtual Machine has not been configured to use the "
+ "desired default character encoding (" + desired + ").");
}
}
}