春天& Maven - 如何为Spring应用程序提供自定义属性

时间:2017-10-24 19:48:58

标签: java spring maven

我有一个在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中的那个?

实现这一目标的“干净”方法是什么?

1 个答案:

答案 0 :(得分:0)