Liquibase - 混合顺序的多个数据源

Liquibase - multiple datasources in a mixed order

据说我们可以 运行 使用以下 spring 个 bean 的多个数据源

<bean id="liquibase1" class="liquibase.integration.spring.SpringLiquibase">
      <property name="dataSource" ref="dataSource1" />
      <property name="changeLog" value="classpath:db1-changelog.xml" />
 </bean>
 <bean id="liquibase2" class="liquibase.integration.spring.SpringLiquibase">
      <property name="dataSource" ref="dataSource2" />
      <property name="changeLog" value="classpath:db2-changelog.xml" />
 </bean>

或使用以下 pom.xml 个人资料

 <profiles>
        <profile>
            <id>db1</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <liquibase.url>jdbc:h2:target/db1/liquibaseTest;AUTO_SERVER=TRUE</liquibase.url>
                <liquibase.driver>org.h2.Driver</liquibase.driver>
                <liquibase.username>user</liquibase.username>
                <liquibase.password>pass</liquibase.password>
            </properties>
        </profile>
        <profile>
            <id>db2</id>
            <properties>
                <liquibase.url>jdbc:h2:target/db2/liquibaseTest;AUTO_SERVER=TRUE</liquibase.url>
                <liquibase.driver>org.h2.Driver</liquibase.driver>
                <liquibase.username>user</liquibase.username>
                <liquibase.password>pass</liquibase.password>
            </properties>
        </profile>
    </profiles>

如果我们需要 运行 混合顺序的脚本怎么办?在我的情况下,我需要 运行 来自 dataSource1 的一个脚本,然后 dataSource2,然后再次 dataSource1,然后再次 dataSource2

我们可以通过多次使用相同的数据源和不同的 bean 注入来做到这一点,如下所示

<bean id="liquibase1" class="liquibase.integration.spring.SpringLiquibase">
      <property name="dataSource" ref="dataSource1" />
      <property name="changeLog" value="classpath:db1-changelog1.xml" />
 </bean>
 <bean id="liquibase2" depends-on="liquibase1" class="liquibase.integration.spring.SpringLiquibase">
      <property name="dataSource" ref="dataSource2" />
      <property name="changeLog" value="classpath:db2-changelog1.xml" />
 </bean>
<bean id="liquibase3" depends-on="liquibase2"  class="liquibase.integration.spring.SpringLiquibase">
      <property name="dataSource" ref="dataSource1" />
      <property name="changeLog" value="classpath:db1-changelog2.xml" />
 </bean>
<bean id="liquibase4" depends-on="liquibase3"  class="liquibase.integration.spring.SpringLiquibase">
      <property name="dataSource" ref="dataSource2" />
      <property name="changeLog" value="classpath:db2-changelog2.xml" />
 </bean>

最好将这些 bean 定义放在单独的 xml 配置文件中并导入它