[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
This commit is contained in:
parent
acce5c0a91
commit
60ce9ba95f
@ -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;
|
||||
}
|
||||
}
|
@ -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"));
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
application.timezone=GMT+08:00
|
@ -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");
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="15 seconds" debug="false">
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>[%d{ISO8601}]-[%thread] %-5level %logger - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
Loading…
x
Reference in New Issue
Block a user