Skip to content

Commit

Permalink
feat : let Spring boot handle container lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Dec 19, 2024
1 parent fc3cce2 commit 8bb92f8
Showing 1 changed file with 2 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.multipledatasources.common;

import jakarta.annotation.PreDestroy;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.DynamicPropertyRegistrar;
Expand All @@ -16,22 +15,14 @@
@TestConfiguration(proxyBeanMethods = false)
public class ContainersConfiguration {

private MySQLContainer<?> mySQLContainer;
private PostgreSQLContainer<?> postgreSQLContainer;

@Bean
MySQLContainer<?> mySQLContainer() {
mySQLContainer = new MySQLContainer<>(DockerImageName.parse("mysql").withTag("9.1"));
mySQLContainer.start();
return mySQLContainer;
return new MySQLContainer<>(DockerImageName.parse("mysql").withTag("9.1"));
}

@Bean
PostgreSQLContainer<?> postgreSQLContainer() {
postgreSQLContainer =
new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("17.2-alpine"));
postgreSQLContainer.start();
return postgreSQLContainer;
return new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("17.2-alpine"));
}

@Bean
Expand All @@ -47,14 +38,4 @@ public DynamicPropertyRegistrar databaseProperties(
properties.add("spring.datasource.password", postgreSQLContainer::getPassword);
};
}

@PreDestroy
public void cleanup() {
if (mySQLContainer != null && mySQLContainer.isRunning()) {
mySQLContainer.stop();
}
if (postgreSQLContainer != null && postgreSQLContainer.isRunning()) {
postgreSQLContainer.stop();
}
}
}

0 comments on commit 8bb92f8

Please sign in to comment.