使用Spring配置初始化默认的Locale和Timezone [英] Initialize default Locale and Timezone with Spring configuration
问题描述
我正在使用PropertyPlaceholderConfigurer从属性文件加载应用程序设置,例如JDBC连接信息。我还想将其他设置(如默认语言环境和时区)作为属性。
I'm loading application settings such as JDBC connection info from a properties file using PropertyPlaceholderConfigurer. I'd like to also have other settings such as default locale and timezone as properties.
但我不确定执行 Locale.setDefault()
和 TimeZone.setDefault()
。我希望他们在启动时早点运行一次。在其他代码执行之前,Spring是否有正确的方法执行某些代码?有什么建议吗?
But I'm unsure of the best method to execute Locale.setDefault()
and TimeZone.setDefault()
. I want them run early in startup and only once. Is there a proper way in Spring to execute some code FIRST, before other code is executed? Any suggestions?
我知道我可以在命令行中指定默认值,但是这个应用程序将在很多地方安装,我想避免因忘记的人造成的问题指定-Duser.timezone = UTC或其他。
I know I can specify default values on the command line, but this application will get installed in many places and I want to avoid problems caused by someone forgetting to specify -Duser.timezone=UTC or whatever.
推荐答案
我使用了 ServletContextListener
。在 contextInitialized(..)
调用TimeZone.setDefault(..)
。
如果您依赖任何构造函数中的时区或 @PostConstruct
/ afterPropertiesSet(),则不会将其考虑在内
但是。
It won't be taken into account if you rely on the timezone in any constructor or @PostConstruct
/ afterPropertiesSet()
though.
如果需要,请查看这个问题
这篇关于使用Spring配置初始化默认的Locale和Timezone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!