Trong Spring Boot 3.x, annotation @ServiceConnection ở đoạn code sau giúp gì?
@SpringBootTest
@Testcontainers
class ProductRepositoryIT {
@Container
@ServiceConnection
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine");
// ...
}
Xem đáp án và giải thích
Trước Boot 3.1, phải khai báo @DynamicPropertySource để map postgres.getJdbcUrl()... vào spring.datasource.*. @ServiceConnection để Boot nhận diện loại container (PostgreSQL, MySQL, Redis, Kafka, RabbitMQ...) và tạo bean ConnectionDetails tương ứng, được ưu tiên hơn property thông thường. Cần dependency spring-boot-testcontainers và module Testcontainers phù hợp.