SpringBoot整合Liquibase
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
@SpringBootApplication
public class MySpringBootApplication {
// 仅在开发环境中运行Liquibase更新
@Bean
@Profile("dev")
public SpringLiquibase liquibase(DataSource dataSource) {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setDataSource(dataSource);
liquibase.setChangeLog("classpath:db/changelog/db.changelog-master.yaml");
liquibase.setContexts("development");
return liquibase;
}
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}
这段代码演示了如何在Spring Boot应用程序中使用Liquibase。它定义了一个SpringLiquibase Bean,该Bean在开发环境(dev profile)中运行,并指向了Liquibase的更改日志文件。在生产环境中,通常不需要Liquibase,因此不会创建SpringLiquibase Bean,或者只在特定的Profile(例如"prod")中创建。
评论已关闭