版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/andy_zhang2007/article/details/84173325
源码分析
本文源代码基于 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 + ").");
}
}
}
相关文章
Springboot内置ApplicationListener