diff --git a/spring-boot-modules/spring-boot-properties-3/pom.xml b/spring-boot-modules/spring-boot-properties-3/pom.xml index d72c410d9b..3de85c7175 100644 --- a/spring-boot-modules/spring-boot-properties-3/pom.xml +++ b/spring-boot-modules/spring-boot-properties-3/pom.xml @@ -19,6 +19,10 @@ org.springframework.boot spring-boot-starter + + org.springframework.boot + spring-boot-starter-actuator + org.springframework.boot spring-boot-starter-test diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/AppContextRefreshedEventPropertiesPrinter.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/AppContextRefreshedEventPropertiesPrinter.java new file mode 100644 index 0000000000..342371be45 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/AppContextRefreshedEventPropertiesPrinter.java @@ -0,0 +1,55 @@ +package com.baeldung.properties.log; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.event.EventListener; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.stereotype.Component; + +import java.util.Collection; + +@Component +public class AppContextRefreshedEventPropertiesPrinter { + private static final Logger LOGGER = LoggerFactory.getLogger(AppContextRefreshedEventPropertiesPrinter.class); + + @EventListener + public void handleContextRefreshed(ContextRefreshedEvent event) { + printAllActiveProperties((ConfigurableEnvironment) event.getApplicationContext().getEnvironment()); + + printAllApplicationProperties((ConfigurableEnvironment) event.getApplicationContext().getEnvironment()); + } + + private void printAllActiveProperties(ConfigurableEnvironment env) { + + LOGGER.info("************************* ALL PROPERTIES(EVENT) ******************************"); + + env.getPropertySources() + .stream() + .filter(ps -> ps instanceof MapPropertySource) + .map(ps -> ((MapPropertySource) ps).getSource().keySet()) + .flatMap(Collection::stream) + .distinct() + .sorted() + .forEach(key -> LOGGER.info("{}={}", key, env.getProperty(key))); + + LOGGER.info("******************************************************************************"); + } + + private void printAllApplicationProperties(ConfigurableEnvironment env) { + + LOGGER.info("************************* APP PROPERTIES(EVENT) ******************************"); + + env.getPropertySources() + .stream() + .filter(ps -> ps instanceof MapPropertySource && ps.getName().contains("application.properties")) + .map(ps -> ((MapPropertySource) ps).getSource().keySet()) + .flatMap(Collection::stream) + .distinct() + .sorted() + .forEach(key -> LOGGER.info("{}={}", key, env.getProperty(key))); + + LOGGER.info("******************************************************************************"); + } +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/EnvironmentPropertiesPrinter.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/EnvironmentPropertiesPrinter.java new file mode 100644 index 0000000000..321593b31b --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/EnvironmentPropertiesPrinter.java @@ -0,0 +1,29 @@ +package com.baeldung.properties.log; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +@Component +public class EnvironmentPropertiesPrinter { + private static final Logger LOGGER = LoggerFactory.getLogger(EnvironmentPropertiesPrinter.class); + private final Environment env; + + public EnvironmentPropertiesPrinter(Environment env) { + this.env = env; + } + + @PostConstruct + public void logApplicationProperties() { + LOGGER.info("************************* PROPERTIES(ENVIRONMENT) ******************************"); + + LOGGER.info("{}={}", "bael.property", env.getProperty("bael.property")); + LOGGER.info("{}={}", "app.name", env.getProperty("app.name")); + LOGGER.info("{}={}", "app.description", env.getProperty("app.description")); + + LOGGER.info("******************************************************************************"); + } +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/LogPropertiesDemoApplication.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/LogPropertiesDemoApplication.java new file mode 100644 index 0000000000..642a03edd6 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/LogPropertiesDemoApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.properties.log; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class LogPropertiesDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(LogPropertiesDemoApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml index 0f048ffa14..2e28da5ad3 100644 --- a/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml +++ b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml @@ -55,3 +55,9 @@ application: uk: 14 fr: 42 us: 10 +--- +management: + endpoints: + web: + exposure: + include: env, health \ No newline at end of file