我发现Spring在调用上下文初始化方法之前加载了一些默认的bean,包括其他bean,所以,这是一个更好的方法“draft”,如果你能想到,如果你看到任何担忧,请告诉我:
public class SystemPropertyDefaultsInitializer
implements WebApplicationInitializer{
private static final Logger logger = Logger
.getLogger(SystemPropertyDefaultsInitializer.class);
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
logger.info("SystemPropertyWebApplicationInitializer onStartup called");
// can be set runtime before Spring instantiates any beans
// TimeZone.setDefault(TimeZone.getTimeZone("GMT+00:00"));
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
// cannot override encoding in Spring at runtime as some strings have already been read
// however, we can assert and ensure right values are loaded here
// verify system property is set
Assert.isTrue("UTF-8".equals(System.getProperty("file.encoding")));
// and actually verify it is being used
Charset charset = Charset.defaultCharset();
Assert.isTrue(charset.equals(Charset.forName("UTF-8")));
// locale
// set and verify language
}
}