【问题标题】:Application context listener doesn't work after migrating to Spring Boot 2.0迁移到 Spring Boot 2.0 后应用程序上下文侦听器不起作用
【发布时间】:2018-07-20 10:19:44
【问题描述】:

我们最近将 Spring Boot 从 1.5 迁移到 2.0。迁移后,当我运行应用程序时,会调用自定义上下文侦听器,如果我们在尚未创建的源之前添加属性源,则会在其中使用exit code 1 停止应用程序。

在 Spring Boot 1.5.x 中,ConfigFileApplicationListenerapplicationConfigurationProperties 添加到可用属性源列表中。

由于某种原因,applicationConfigurationProperties 在迁移后没有被添加,因此以下代码失败:

event.getEnvironment().getPropertySources().addBefore("applicationConfigurationProperties", new MapPropertySource("aws", parameters));

使用 Spring 1.5.x

o.s.w.c.s.StandardServletEnvironment:Adding PropertySource 'aws' with search precedence immediately higher than 'applicationConfigurationProperties'

我也启用了以下日志,但没有看到任何异常:

logging.level.org.springframework=TRACE
logging.level.org.springframework.context=TRACE
logging.level.org.springframework.boot=TRACE
logging.level.org.springframework.core=TRACE

所有应用程序侦听器都实现ApplicationListener<ApplicationEnvironmentPreparedEvent>。 我查看了迁移指南,发现添加了一个新事件,ApplicationStartedEvent,但这应该没有任何区别。

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    在 Spring Boot 2 中没有名为 applicationConfigurationProperties 的属性源。从配置文件派生的属性源的名称现在包括文件所在位置的详细信息。例如,如果您在类路径的根目录中有一个application.properties 文件,那么您将有一个名为applicationConfig: [classpath:/application.properties] 的属性源。

    【讨论】:

    • 谢谢。我很难弄明白。我通过在RandomValuePropertySource.RANDOM_PROPERTY_SOURCE_NAME 之后而不是ConfigFileApplicationListener.APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME 之前添加属性源来修复代码。我想知道 Spring 团队为什么会做出这样的改变。顺便说一句,他们忘记从 ConfigFileApplicationListener 中删除该常量。
    • 在源名称中包含加载属性的位置,使 Acuator 的 /env 端点等内容更加有用。感谢指向旧常量的指针。我已经打开 an issue 来弃用它。
    猜你喜欢
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 2020-02-27
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多