Spring Boot 1.4: Executing Method after Liquibase finished

情到浓时终转凉″ 提交于 2020-05-11 04:56:31

问题


I have a Spring Boot 1.4.0 based Project that uses Liquibase.

Is it possible to execute a Method AFTER liquibase finished?

Something like Bean Post Processor?

What i want to do is adding some data to my database when the application is started in development mode. In developement mode the application uses an in-memory h2 database, so liquibase has to create the tables before i can write my data.


回答1:


Spring Boot auto-configures a SpringLiquibase bean named liquibase. Any bean that depends on this bean will be created after Liquibase has finished. For example, you could use @PostConstruct to populate the database:

@Bean
@DependsOn("liquibase")
public YourBean yourBean() {
    return new YourBean();
}

static class YourBean {

    @PostConstruct
    public void populateDatabase() {
        System.out.println("This will be called after Liquibase has finished");
    }

}



回答2:


Another solution would be to let LiquiBase insert these things into your database - but only when running in dev-mode.

You can do this in LiquiBase by specifying a context="" attribute.

Another option would be to let LiquiBase only insert this test-data into your database when dbms="h2db" (forgot what the exact string is to select h2, check documentation please!)

Both are attributes on changesets.

This is my own preferred solution for this kind of scenarios.



来源:https://stackoverflow.com/questions/38825670/spring-boot-1-4-executing-method-after-liquibase-finished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!