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.
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?
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.
I've used a
ServletContextListener
. IncontextInitialized(..)
TimeZone.setDefault(..)
is called.It won't be taken into account if you rely on the timezone in any constructor or
@PostConstruct
/afterPropertiesSet()
though.If you need it, take a look at this question
How about standalone spring boot application? The java application looks like:
I found Spring loads some of its default beans including other beans before calling the contextInitialized method, so, here is a better approach "draft" that I can think of, let me know if you see any concern: