@AutoConfigureBefore 不会触发 [英] @AutoConfigureBefore does not trigger

查看:43
本文介绍了@AutoConfigureBefore 不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个项目的目的是在 Liquibase 执行其数据库架构之前,创建挂钩到 Spring Boot 的生命周期变更集.钩子最终将用于启动/停止带有 Postgres 实例的 Docker(或类似 Docker 的)容器.项目必须能够处理:

The intention of this project is to create hooks into Spring Boot's lifecycle, right before Liquibase executes its database schema changesets. The hooks will eventually be used to start / stop a Docker (or Docker-like) container with a Postgres instance. The project must be able to deal with:

  • 从 IDE(IntellIJ、STS)运行测试
  • 使用 Maven Surefire 运行测试
  • spring-boot:run
  • 可执行 JAR
  • CI 服务器上的集成测试

鉴于上述限制,最好的方法似乎是使用自动配置并指示它在 Liquibase 之前运行.

Given the above limitations, the best approach appeared to be to have an Auto-Configuration and instructing it to run before Liquibase.

自动配置类 已注解:

@ConditionalOnProperty(prefix = "docker_42", 
    name = "enabled", matchIfMissing = false)
@AutoConfigureBefore({LiquibaseAutoConfiguration.class })
public class Docker42AutoConfiguration {

spring.factories 有一个条目:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
nl._42.autoconfig.Docker42AutoConfiguration

整个(精简)项目可以在这里找到:https://github.com/robert-bor/auto-configuration-question

The entire (stripped down) project can be found here: https://github.com/robert-bor/auto-configuration-question

结果可以通过以下任一方式验证:

The results can be verified by either:

  • 在您的 IDE 中设置项目 &运行测试,或
  • 执行 spring-boot:run

在 Spring Boot 的日志中,您将看到 Liquibase 在自定义 AutoConfiguration 之前执行.

In Spring Boot's log you will see Liquibase executing before the custom AutoConfiguration.

请注意,已经尝试了各种其他路线(ApplicationListenerRunListener),但没有一条路线在所有必需的道路上都表现良好.

Note that various other routes have been tried (ApplicationListener, RunListener), but none played nice with all the required in-roads.

关于为什么 @AutoConfigureBefore 在我的项目中不起作用的指针将不胜感激.

Pointers as to why @AutoConfigureBefore does not work in my project would be very much appreciated.

推荐答案

您正在尝试将配置语义应用于某些运行时约束.自动配置是关于在上下文中提供 bean 定义,最终会创建可以在组件中连接的 bean 实例.

You're trying to apply a configuration semantic to some runtime constraints. Auto-configuration is about providing bean definitions in the context, something that will ultimately create bean instances that you can wire in your components.

假设你需要 bean Foo 来自动配置一些东西.您需要确保 FooAutoConfiguration 在您的之前运行,以便上下文有机会包含 Foo 的 bean 定义.这在我们的最后一所德沃克斯大学中有非常详细的解释.

Assume that you need bean Foo in order to auto-configure something. You need to make sure that FooAutoConfiguration runs before yours so that the context has a chance to contain a bean definition for Foo. That is explained in quite details in our last Devoxx university.

您在该评论中引用的文档并不以任何方式暗示运行时限制:

The documentation you referenced in that comment does not imply runtime constraints in any way:

提示应该在其他指定的自动配置类之前应用自动配置.

Hint for that an auto-configuration should be applied before other specified auto-configuration classes.

应用自动配置并不意味着该配置创建的 bean 将在另一个配置创建的 bean 之前有效启动.换句话说,您所做的是确保您的自动配置在 liquibase 自动配置有机会这样做之前配置了上下文.无论如何,这并不意味着将要创建的 bean(我们还没有)将按照该顺序创建.

applying the auto-configuration does not mean that the beans created by that configuration will effectively start before the beans created by another configuration. In other words, what you've done is making sure that your auto-configuration configures the context before the liquibase auto-configuration has a chance to do so. It does not imply, in any way, that the beans that will be created (we're not there yet) will be created in that order.

如果这就是您想要做的并且您没有 bean 依赖项,您可以忘记所有这些并创建一个 bean 生命周期依赖项.这可能很棘手,因为 liquibase 可能存在也可能不存在,但我们基本上为 Hazelcast 这样做(参见 一>).基本上我们需要确保在启动 JPA 容器之前启动 Hazelcast,以防它想使用 Hazelcast 作为二级缓存.

If that's what you want to do and you have no bean dependency, you can just forget about all that and create a bean lifecycle dependency instead. It can be tricky because liquibase may or may not be there but we essentially do that for Hazelcast (see the auto-configuration). Basically we need to make sure that Hazelcast is started before the JPA container is started in case it wants to use Hazelcast as second level cache.

这篇关于@AutoConfigureBefore 不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆