From 60ce9ba95f0450c5e9e201e5026472aeab4f5f12 Mon Sep 17 00:00:00 2001 From: Stelios Anastasakis Date: Thu, 18 Apr 2024 20:21:48 +0300 Subject: [PATCH] [BAEL-7669] Example code for article. Setting global TimeZone in spring (#16260) * [BAEL-7669] Example code for article. Setting global TimeZone in spring * Added logback-test.xml for spring tests, to set level to INFO * [BAEL-7669] Switched to spring-boot-application to include more examples * Added things needed in pom.xml, without affecting existing versions * [BAEL-7669] After code review: exported spring boot version in property - Fixed constant name, to follow the uppercase convention - Added comments to explain the commented out options, as described in the article * [BAEL-7669] Added example of reading timezone value from properties file * [BAEL-7669] Moved code to spring-boot-customization module --- .../globaltimezone/GlobalTimeZoneBean.java | 28 ++++++++++++++ ...lobalTimezoneBeanFactoryPostProcessor.java | 24 ++++++++++++ .../globaltimezone/MainApplication.java | 37 +++++++++++++++++++ .../src/main/resources/application.properties | 1 + ...ezoneBeanFactoryPostProcessorUnitTest.java | 19 ++++++++++ .../src/test/resources/logback-test.xml | 12 ++++++ 6 files changed, 121 insertions(+) create mode 100644 spring-boot-modules/spring-boot-basic-customization-3/src/main/java/com/baeldung/globaltimezone/GlobalTimeZoneBean.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-3/src/main/java/com/baeldung/globaltimezone/GlobalTimezoneBeanFactoryPostProcessor.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-3/src/main/java/com/baeldung/globaltimezone/MainApplication.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-3/src/test/java/com/baeldung/globaltimezone/GlobalTimezoneBeanFactoryPostProcessorUnitTest.java create mode 100644 spring-boot-modules/spring-boot-basic-customization-3/src/test/resources/logback-test.xml diff --git a/spring-boot-modules/spring-boot-basic-customization-3/src/main/java/com/baeldung/globaltimezone/GlobalTimeZoneBean.java b/spring-boot-modules/spring-boot-basic-customization-3/src/main/java/com/baeldung/globaltimezone/GlobalTimeZoneBean.java new file mode 100644 index 0000000000..a055d27083 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-3/src/main/java/com/baeldung/globaltimezone/GlobalTimeZoneBean.java @@ -0,0 +1,28 @@ +package com.baeldung.globaltimezone; + +import java.util.TimeZone; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +@Component +public class GlobalTimeZoneBean { + + private static final Logger LOGGER = LoggerFactory.getLogger(GlobalTimeZoneBean.class); + + private final String globalTimeZone; + + public GlobalTimeZoneBean() { + // TimeZone.setDefault(TimeZone.getTimeZone("GMT+08:00")); + LOGGER.info("Default timezone, during beans creation, is set to: " + TimeZone.getDefault() + .getDisplayName()); + + globalTimeZone = TimeZone.getDefault() + .getDisplayName(); + } + + public String getGlobalTimeZone() { + return globalTimeZone; + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization-3/src/main/java/com/baeldung/globaltimezone/GlobalTimezoneBeanFactoryPostProcessor.java b/spring-boot-modules/spring-boot-basic-customization-3/src/main/java/com/baeldung/globaltimezone/GlobalTimezoneBeanFactoryPostProcessor.java new file mode 100644 index 0000000000..eafcf092f4 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-3/src/main/java/com/baeldung/globaltimezone/GlobalTimezoneBeanFactoryPostProcessor.java @@ -0,0 +1,24 @@ +package com.baeldung.globaltimezone; + +import java.util.TimeZone; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.stereotype.Component; + +@Component +public class GlobalTimezoneBeanFactoryPostProcessor implements BeanFactoryPostProcessor { + + private static final Logger LOGGER = LoggerFactory.getLogger(GlobalTimezoneBeanFactoryPostProcessor.class); + + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + LOGGER.info("Default timezone, before bean factory post processor, is set to: " + TimeZone.getDefault() + .getDisplayName()); + + TimeZone.setDefault(TimeZone.getTimeZone("GMT+08:00")); + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization-3/src/main/java/com/baeldung/globaltimezone/MainApplication.java b/spring-boot-modules/spring-boot-basic-customization-3/src/main/java/com/baeldung/globaltimezone/MainApplication.java new file mode 100644 index 0000000000..f09448bad7 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-3/src/main/java/com/baeldung/globaltimezone/MainApplication.java @@ -0,0 +1,37 @@ +package com.baeldung.globaltimezone; + +import java.util.TimeZone; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import jakarta.annotation.PostConstruct; + +@SpringBootApplication +public class MainApplication { + + @Value("${application.timezone:UTC}") + private String applicationTimeZone; + + private static final Logger LOGGER = LoggerFactory.getLogger(MainApplication.class); + + public static void main(String[] args) { + // For having the value available from the very early stages of the execution, even before detecting the Spring Profile, set the value here: + // TimeZone.setDefault(TimeZone.getTimeZone("GMT+08:00")); + LOGGER.info("Default timezone, before main run, is set to: " + TimeZone.getDefault() + .getDisplayName()); + + SpringApplication.run(MainApplication.class, args); + } + + @PostConstruct + public void executeAfterMain() { + // For having the value available just after WebApplicationContext initialization is completed and using application properties: + // TimeZone.setDefault(TimeZone.getTimeZone(applicationTimeZone)); + LOGGER.info("Default timezone, after main run, is set to: " + TimeZone.getDefault() + .getDisplayName()); + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization-3/src/main/resources/application.properties b/spring-boot-modules/spring-boot-basic-customization-3/src/main/resources/application.properties index e69de29bb2..6977a1de0a 100644 --- a/spring-boot-modules/spring-boot-basic-customization-3/src/main/resources/application.properties +++ b/spring-boot-modules/spring-boot-basic-customization-3/src/main/resources/application.properties @@ -0,0 +1 @@ +application.timezone=GMT+08:00 diff --git a/spring-boot-modules/spring-boot-basic-customization-3/src/test/java/com/baeldung/globaltimezone/GlobalTimezoneBeanFactoryPostProcessorUnitTest.java b/spring-boot-modules/spring-boot-basic-customization-3/src/test/java/com/baeldung/globaltimezone/GlobalTimezoneBeanFactoryPostProcessorUnitTest.java new file mode 100644 index 0000000000..f9109fa42d --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-3/src/test/java/com/baeldung/globaltimezone/GlobalTimezoneBeanFactoryPostProcessorUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.globaltimezone; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest(classes = MainApplication.class) +class GlobalTimezoneBeanFactoryPostProcessorUnitTest { + + @Autowired + private GlobalTimeZoneBean globalTimeZoneBean; + + @Test + void givenUTCTimeZoneSetInBeanFactoryPostProcessor_whenRetrieveGlobalTimeZone_thenReturnUTC() { + assertThat(globalTimeZoneBean.getGlobalTimeZone()).isEqualTo("GMT+08:00"); + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization-3/src/test/resources/logback-test.xml b/spring-boot-modules/spring-boot-basic-customization-3/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..e30138a422 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization-3/src/test/resources/logback-test.xml @@ -0,0 +1,12 @@ + + + + + [%d{ISO8601}]-[%thread] %-5level %logger - %msg%n + + + + + + +