In the previous tutorial, I introduced you to Flyway for database migration. Integrating Flyway with the Spring Framework will make it easier for you to work with Flyway in applications that use the Spring Framework. In this tutorial, I will introduce you to the ways to integrate Flyway with the Spring Framework!
Setting up the project
First, I will create a new Maven project as an example:

The Spring Framework, Flyway, and PostgreSQL JDBC driver dependencies are declared as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>${flyway.version}</version> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-database-postgresql</artifactId> <version>${flyway.version}</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>${postgresql.version}</version> </dependency> |
with:
|
1 2 3 4 5 6 |
<properties> ... <spring.version>7.1.0</spring.version> <flyway.version>13.13.0</flyway.version> <postgresql.version>42.7.14</postgresql.version> </properties> |
I will also create a new SQL file named V2026.11.28.00000__Init in the /src/main/resources/db/migration directory with the following content to create a new students table:
|
1 2 3 4 |
CREATE TABLE students ( ID INT PRIMARY KEY, name VARCHAR(250) NOT NULL ); |
We will use an object of the Flyway class to perform database migration. With the Spring Framework, you can define the bean of this Flyway class as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.huongdanjava.spring; import javax.sql.DataSource; import org.flywaydb.core.Flyway; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DriverManagerDataSource; @Configuration public class AppConfig { @Bean public DataSource dataSource() { return new DriverManagerDataSource( "jdbc:postgresql://localhost:5431/spring_flyway_example", "khanh", "123456"); } @Bean public Flyway flyway(DataSource dataSource) { return Flyway.configure() .dataSource(dataSource) .load(); } } |
Migrate the database using the initMethod property of the @Bean annotation.
To call the migrate() method of the Flyway object and perform database migration, you can use the initMethod property of the @Bean annotation as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.huongdanjava.spring; import javax.sql.DataSource; import org.flywaydb.core.Flyway; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DriverManagerDataSource; @Configuration public class AppConfig { @Bean public DataSource dataSource() { return new DriverManagerDataSource( "jdbc:postgresql://localhost:5431/spring_flyway_example", "khanh", "123456"); } @Bean(initMethod = "migrate") public Flyway flyway(DataSource dataSource) { return Flyway.configure() .dataSource(dataSource) .load(); } } |
With this method, when you initialize the Flyway bean, Spring will automatically call the migrate() method of the Flyway object to execute the database migration.
Migrating the database using the InitializingBean interface implementation
Another way you can do this is to define a class that implements Spring’s InitializingBean interface, for example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.huongdanjava.spring; import org.flywaydb.core.Flyway; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; @Component public class FlywayMigrationInitializer implements InitializingBean { private final Flyway flyway; public FlywayMigrationInitializer(Flyway flyway) { this.flyway = flyway; } @Override public void afterPropertiesSet() { flyway.migrate(); } } |
As you can see above, we will call the migrate() method of the Flyway object in the afterPropertiesSet() method. After initializing the bean and setting its properties, Spring will call this migrate() method to run the database migration.
I will use the first method for this example.
Running database migration
Now we can write the code to run the application with the Spring Framework as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Application { static void main(String[] args) { try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class)) { // Application starts here } } } |
Run this example, and you’ll see the same result as when we work with Flyway:

Summary
In this tutorial, I’ve shared with you several ways to integrate Flyway with the Spring Framework. Understanding these integration methods will help you better understand the auto-configuration mechanism of Spring Boot for Flyway. I’ll discuss this in a future tutorial!



