Get the name of the file downloaded by @PropertySource

In any case, I can find out in my program the full path to the file downloaded via the @PropertySource Spring annotation. I need it to appear in the logs so that I can find out which properties file is used in the application

+4
source share
2 answers

The below seems to work, although I'm not sure if the instance always has type ConfigurableEnvironment

@Component
public class MyListener implements ApplicationListener<ContextRefreshedEvent>{

  @Autowired
  private Environment env;

  private static final Logger log = LoggerFactory.getLogger(MyListener.class);

  @Override
  public void onApplicationEvent(ContextRefreshedEvent event) {

    if(env instanceof ConfigurableEnvironment){
      MutablePropertySources propertySources = ((ConfigurableEnvironment)env).getPropertySources();
      for(PropertySource ps : propertySources){
        log.info(ps.getName());  //if only file based needed then check if instanceof ResourcePropertySource
      }
    }
  }
}

Edit: don't need all of this. As Selim already answered, turning on the right magazines does the trick

log4j.logger.org.springframework.core.env.MutablePropertySources=DEBUG

+2
source

StandardServletEnvironment. DEBUG org.springframework.web.context.support.StandardServletEnvironment, .

spring -boot, application.properties.

logging.level.org.springframework.web.context.support.StandardServletEnvironment = DEBUG
+2

Source: https://habr.com/ru/post/1621431/


All Articles