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 + + + + + + +