获取@PropertySource 加载的文件名 [英] get name of the file loaded by @PropertySource
本文介绍了获取@PropertySource 加载的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无论如何我可以在我的程序中知道通过Spring的@PropertySource注释加载的文件的完整路径.我需要它显示在日志中,以便人们可以知道应用程序中正在使用哪个属性文件
Is there anyway I can know in my program, the full path of file loaded through @PropertySource annotation of Spring. I need it to show in logs so that one can know which property file is being used in the application
推荐答案
以下似乎有效,但我不确定该实例是否始终为 ConfigurableEnvironment
Below seems to be working, though I am not sure if the instance is always of 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
}
}
}
}
真的不需要这一切.正如塞利姆已经回答的那样,启用正确的日志可以解决问题
don't really need all this. As already answered by Selim, enabling the proper logs does the trick
log4j.logger.org.springframework.core.env.MutablePropertySources=DEBUG
这篇关于获取@PropertySource 加载的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文