-
-
Save sandor-nemeth/f6d2899b714e017266cb9cce66bc719d to your computer and use it in GitHub Desktop.
package io.github.sandornemeth.spring; | |
import java.util.Arrays; | |
import java.util.stream.StreamSupport; | |
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.AbstractEnvironment; | |
import org.springframework.core.env.EnumerablePropertySource; | |
import org.springframework.core.env.Environment; | |
import org.springframework.core.env.MutablePropertySources; | |
import org.springframework.stereotype.Component; | |
@Component | |
public class PropertyLogger { | |
private static final Logger LOGGER = LoggerFactory.getLogger(PropertyLogger.class); | |
@EventListener | |
public void handleContextRefresh(ContextRefreshedEvent event) { | |
final Environment env = event.getApplicationContext().getEnvironment(); | |
LOGGER.info("====== Environment and configuration ======"); | |
LOGGER.info("Active profiles: {}", Arrays.toString(env.getActiveProfiles())); | |
final MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources(); | |
StreamSupport.stream(sources.spliterator(), false) | |
.filter(ps -> ps instanceof EnumerablePropertySource) | |
.map(ps -> ((EnumerablePropertySource) ps).getPropertyNames()) | |
.flatMap(Arrays::stream) | |
.distinct() | |
.filter(prop -> !(prop.contains("credentials") || prop.contains("password"))) | |
.forEach(prop -> LOGGER.info("{}: {}", prop, env.getProperty(prop))); | |
LOGGER.info("==========================================="); | |
} | |
} |
Perfect ! Its really useful.
Thanks!
Perfect, thanks to you to have share this piece of code.
Thanks 🚀
But with this approach we are only being able to print the ENV variables and not the actual variables which are annotated with @value (edited)
Very nice
Good one
Hey this is great - thanks so much for posting. I'm using this to take application properties from Spring and map them into system properties that are used by an embedded third party application.
By that we can see what property values are really in effect when running the application. I used it with some modifications to see the property values in effect when running a test case. Thank you very much.
Doesn't work if there is a configuration problem (e.g. a bean can't initialize due to a missing property, then the APPLICATION FAILED TO START); it's not even executed...
Thank you!
Doesn't work if there is a configuration problem (e.g. a bean can't initialize due to a missing property, then the APPLICATION FAILED TO START); it's not even executed...
Worked for me with Spring Boot 2.6.3.
I created a placeholder for a non existant property @Value("${pepe}") private String pepe;
and got this.
17:10:59.488 [main] ERROR o.s.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'generalInfo': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'pepe' in value "${pepe}"
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'pepe' in value "${pepe}"
Worked for me with Spring Boot 2.6.3.
OK great then!! the project I was working on used v2.5.3 so seems it's been fixed since then...
you saved the day! thanks
Works like a charm with Spring Boot 2.7.3
Works great. But when the application crashes, it is not called, as already mentioned. This can be fixed, with a listener, which is manually added to the spring application. See here
https://gist.github.com/Christian-Oette/a0bb05cb703fea20d28bbf46b7ce7574
How can I use this code in the public static void main(String[] args)
method on startup?
very useful, thanks a lot.
I improved it a bit by also calling .sorted() after .filter().