使用@ServiceConnection时spring.datasource.hikari.connectionInitSql被忽略问题
问题:Spring Boot Service Connection 忽略 Hikari connectionInitSql 配置
我有一个基于Spring Boot 3.4.3、使用PostgreSQL数据库并启用列加密的应用,在application.properties中配置了:
spring.datasource.hikari.connectionInitSql=select set_config('encryption.key', 'some-password', false)
应用启动时会执行Liquibase迁移,其中一个迁移用到了current_setting('encryption.key')。
我尝试使用Service Connection特性,将Testcontainers数据库作为开发库,但启动时报错:
ERROR: unrecognized configuration parameter "encryption.key"
该错误表明spring.datasource.hikari.connectionInitSql的配置似乎被忽略了。
使用传统的@Container和@DynamicPropertySource方式时,应用可以正常运行:
@Testcontainers @SpringBootTest class MyApplicationTests2 { @Container static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:latest"); @DynamicPropertySource static void configureProperties(DynamicPropertyRegistry registry) { registry.add("spring.datasource.url", postgres::getJdbcUrl); registry.add("spring.datasource.username", postgres::getUsername); registry.add("spring.datasource.password", postgres::getPassword); } @Autowired UserRepository userRepository; @Test void contextLoads() { System.out.println(userRepository.count()); } }
我采用Service Connection的实现代码如下:
@TestConfiguration(proxyBeanMethods = false) class TestcontainersConfiguration { @Bean @ServiceConnection PostgreSQLContainer<?> postgresContainer() { return new PostgreSQLContainer<>("postgres:latest"); } } @Import(TestcontainersConfiguration.class) @SpringBootTest class MyApplicationTests { @Autowired UserRepository userRepository; @Test void contextLoads() { System.out.println(userRepository.count()); } } public class TestMyApplication { public static void main(String[] args) { SpringApplication.from(SpdConfigServerApplication::main).with(TestcontainersConfiguration.class).run(args); } }
内容的提问来源于stack exchange,提问作者FkJ
相关产品推荐
相关产品推荐

