如何将自测集成到Spring-集成中?

How to integrate a self-test into Spring-Integration?

我想在spring-集成开始后开始自检。我的第一个方法是在设置集成流程后启动它:

@Configuration
@EnableIntegration
@EnableIntegrationManagement
@IntegrationComponentScan
public class FlowConfig {
...
    @PostConstruct
    public void startSelfTest() {
        SelfTest selfTest = new SelfTest(rezeptConfig, dataSource, archiveClient);
        selfTest.run();
    }
...
}

这不起作用,因为当测试开始时,数据库中的表丢失了,因为 liquibase 尚未启动。我想 liquibase 脚本会在初始化后启动。

有什么想法是开始自测的最佳位置吗?

只是猜测,ApplicationListener 中的 onApplicationEvent 事件怎么样?这在 Spring 初始化并准备就绪时调用。

例如检查这个 How to add a hook to the application context initialization event?

负责创建和更新数据库表的 Liquibase bean 在我的 Selftest 之后启动。一种解决方案是将@DependsOn 与@Bean 注释一起使用:

    @Bean
    @DependsOn("liquibase")
    public SelfTest startSelfTest() {
        SelfTest selfTest = new SelfTest(rezeptConfig, dataSource, archiveClient);
        selfTest.run();
        return selfTest;
    }

现在在 Liquibase 之后开始自检。

嗯,进行 low-level 资源交互的最佳做法是在应用程序上下文中初始化所有内容时。这就是 bean 根据其 SmartLifecycle 实现启动的阶段。

所以,我建议修改您的解决方案,以完成一些 SmartLifecycle.start()

这正是我们在 Spring 集成中到处做的事情。 (确保我们谈论的是完全相同的 Spring 集成:https://spring.io/projects/spring-integration

在文档中查看更多信息:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-lifecycle-processor