gpt4 book ai didi

spring-boot - Spring Boot : When application. 属性和 application.yml 由 spring boot 应用程序加载

转载 作者:行者123 更新时间:2023-12-02 17:19:17 33 4
gpt4 key购买 nike

我希望记录应用程序正在加载 application-profile.propeties 或 application.yml。怎么做。在哪种方法中,我可以听取它并检测它是成功加载还是失败。

最佳答案

您可以通过添加监听 ApplicationReadyEvent

的组件来记录加载的属性源
@Component
public class LoadedConfigFileListener implements ApplicationListener<ApplicationReadyEvent>, Ordered {

private static final Logger logger = LoggerFactory.getLogger(LoadedConfigFileListener.class);

@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
MutablePropertySources propertySources = event.getApplicationContext().getEnvironment().getPropertySources();
Iterator<PropertySource<?>> propertySourceIterator = propertySources.iterator();
propertySourceIterator.forEachRemaining(propertySource -> logger.info("Successfully loaded: \"{}\" into application context", propertySource.getName()));
}

@Override
public int getOrder() {
return ConfigFileApplicationListener.DEFAULT_ORDER + 1;
}
}

关于spring-boot - Spring Boot : When application. 属性和 application.yml 由 spring boot 应用程序加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44115216/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com