【问题标题】:How can I use Spring boot beans with liquidbase CustomTaskChange如何将 Spring Boot bean 与 liquibase CustomTaskChange 一起使用
【发布时间】:2022-01-26 10:49:11
【问题描述】:

我有 Spring Boot 应用程序。 我想实现liquidbase CustomTaskChange 以运行一些代码。 但是我找不到使用现有 spring bean 或通过 applicationContext 获取它们的方法。

如何将 Spring bean 与 CustomTaskChange 一起使用?

【问题讨论】:

  • @xerx593 谢谢。使用 Option1 或 2 不起作用 CustomTaskChange.execute 在应用程序上下文准备好之前被触发。单例可以工作。我尝试创建一个实现 implements ApplicationContextAware 的单例,但它在 CustomTaskChange.execute 之后调用
  • 那么你的名字,你怎么能“找到一种方法来使用我现有的spring bean或通过applicationContext获取它们”? .. 之前应用程序上下文!

标签: spring-boot liquibase


【解决方案1】:

所以在我看来,您的问题实际上是 Liquibase 在您的所有 bean 首次设置之前运行 CustomTaskChange ?

首先,您可以使用 ApplicationContextAware 通过实现以下任一方法来获取 bean:

// Fetches based on Class only
<T> T getBean(Class<T> requiredType) throws BeanException;

// Fetches based on Class & Bean name (useful when you have multiple beans of same type)
<T> T getBean(String name, Class<T> requiredType) throws BeanException; 

其次,可以使用其他spring注解来控制runtime。

@DependsOn("liquibase") 是常见的,但还有其他的,例如 @Order@Lazy 等。如果您展示更多代码,我可能会提供进一步的帮助。

【讨论】:

    猜你喜欢
    • 2015-12-25
    • 2016-01-29
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2020-11-02
    • 1970-01-01
    • 2021-01-02
    相关资源
    最近更新 更多