我有一个在Web容器(Tomcat)中运行的Spring应用程序。此Spring应用程序使用属性文件来查找数据库JDBC位置:
@Configuration
@PropertySource("classpath:app.properties")
public class MyApplication {
}
在app.properties中,我有:
database.dataSource.url=jdbc:postgresql://localhost:5432/app
现在很容易在运行时获取值:
@Component
class DatabaseConfiguration {
@Value("${database.dataSource.url}")
private String URL;
}
到目前为止,这么好。现在我使用cargo-maven2-plugin
插件在集成测试期间部署WAR。在部署WAR之前,通过docker-maven-plugin
插件将临时PostgreSQL数据库部署到Docker容器中。此实例在自定义的动态端口上运行,而不是通常的5432.此端口由${database.port}
插件填充到docker-maven-plugin
属性中。
这意味着我需要以某种方式动态改变app.properties
以填充此端口。这看起来很糟糕,所以也许有办法通过cargo-maven2-plugin
向我的Spring应用程序提供/覆盖端口,所以我可以使用那个而不是app.properties
中的那个?
实现这一目标的“干净”方法是什么?
答案 0 :(得分:0)
使用@TestPropertySource使用不同的属性文件进行测试。 https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/TestPropertySource.html