Initialize default Locale and Timezone with Spring configuration

前端 未结 4 2098
抹茶落季
抹茶落季 2020-12-30 05:36

I\'m loading application settings such as JDBC connection info from a properties file using PropertyPlaceholderConfigurer. I\'d like to also have other settings

相关标签:
4条回答
  • 2020-12-30 06:01

    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:

    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
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-30 06:01

    It may make sense to ensure that these are set before any beans are processed or initialized, as any beans created before this code is run could end up using the previous system default values. If you are using Spring Boot, you could do this in the main method before even initializing Spring:

    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            Locale.setDefault(Locale.US);
            TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
    
            SpringApplication.run(Application.class, args);
        }
    }
    

    If this needs to be run as part of a context that doesn't call the main method, e.g. in a JUnit integration test, you would need to make sure that it's called:

    @SpringBootTest
    public class ApplicationTest {
        @BeforeClass
        public static void initializeLocaleAndTimeZone() {
            // These could be moved into a separate static method in
            // Application.java, to be called in both locations
            Locale.setDefault(Locale.US);
            TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
        }
    
        @Test
        public void applicationStartsUpSuccessfully() {
        }
    }
    
    0 讨论(0)
  • 2020-12-30 06:07

    How about standalone spring boot application? The java application looks like:

    @SpringBootApplication
    @EnableScheduling
    @EnableConfigurationProperties(TaskProperty.class)
    public class JobApplication {
    
    /*  @Autowired
        private TaskProperty taskProperty;
    */  
        public static void main(String[] args) {
            SpringApplication.run(JobApplication.class, args);
        }
    } 
    
    0 讨论(0)
  • 2020-12-30 06:08

    I've used a ServletContextListener. In contextInitialized(..) 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

    0 讨论(0)
提交回复
热议问题