栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在bean实例化之前,如何记录Spring Boot应用程序的所有活动属性?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在bean实例化之前,如何记录Spring Boot应用程序的所有活动属性?

为此,您需要注册一个

ApplicationListener
ApplicationPreparedEvent
]根据文档,要捕获的事件是:

ApplicationPreparedEvent是一个启动事件,当SpringApplication启动并且ApplicationContext已完全准备但未刷新时,发布该事件。在此阶段将加载Bean定义,并且可以使用环境了。

主要方法如下所示:

public static void main(String[] args) {        SpringApplication springApplication = new SpringApplication(MyApplication.class);        springApplication.addListeners(new PropertiesLogger());        springApplication.run(args);        }

我重用了当前问题中引用的答案的代码,但由于您所获取的上下文尚未刷新,并且环境的结构与应用程序启动后的结构并不完全相同,因此我对其进行了修改。我还按属性来源打印了属性:一个用于系统环境,一个用于系统属性,一个用于应用程序配置属性,等等。请注意,

ApplicationPreparedEvent
可以多次触发,并且我打印了属性只有第一次。有关详细信息,请参见Spring
Boot问题#8899

package com.toto.myapp.util;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.context.event.ApplicationPreparedEvent;import org.springframework.context.ApplicationListener;import org.springframework.core.env.ConfigurableEnvironment;import org.springframework.core.env.EnumerablePropertySource;import org.springframework.core.env.PropertySource;import java.util.linkedList;import java.util.List;public class PropertiesLogger implements ApplicationListener<ApplicationPreparedEvent> {  private static final Logger log = LoggerFactory.getLogger(PropertiesLogger.class);  private ConfigurableEnvironment environment;  private boolean isFirstRun = true;  @Override  public void onApplicationEvent(ApplicationPreparedEvent event) {    if (isFirstRun) {      environment = event.getApplicationContext().getEnvironment();      printProperties();    }    isFirstRun = false;  }  public void printProperties() {    for (EnumerablePropertySource propertySource : findPropertiesPropertySources()) {      log.info("******* " + propertySource.getName() + " *******");      String[] propertyNames = propertySource.getPropertyNames();      Arrays.sort(propertyNames);      for (String propertyName : propertyNames) {        String resolvedProperty = environment.getProperty(propertyName);        String sourceProperty = propertySource.getProperty(propertyName).toString();        if(resolvedProperty.equals(sourceProperty)) {          log.info("{}={}", propertyName, resolvedProperty);        }else {          log.info("{}={} OVERRIDDEN to {}", propertyName, sourceProperty, resolvedProperty);        }      }    }  }  private List<EnumerablePropertySource> findPropertiesPropertySources() {    List<EnumerablePropertySource> propertiesPropertySources = new linkedList<>();    for (PropertySource<?> propertySource : environment.getPropertySources()) {      if (propertySource instanceof EnumerablePropertySource) {        propertiesPropertySources.add((EnumerablePropertySource) propertySource);      }    }    return propertiesPropertySources;  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/443204.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号