替换特定于弹簧配置文件的属性文件中的属性占位符 [英] Replace property placeholders in spring profile-specific properties file

查看:49
本文介绍了替换特定于弹簧配置文件的属性文件中的属性占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Spring Boot 应用程序编写测试,但在特定于配置文件的应用程序属性文件中替换占位符时遇到了奇怪的行为.

I am writing a test for a spring boot application and I am facing a weird behaviour with the placeholder replacement in the profile-specific application properties file.

因此,在我的 settings.xml 中,我的数据库凭据设置如下

So, in my settings.xml I have my DB creds set up like

...
<profiles>
    <profile>
        <properties>
            <db.user>MyLogin</db.user>
            <db.password>MyPassword</db.password>
            <db.url>my_db_connection_url</db.url>
        </properties>
    </profile>
</profiles>
...

所以 settings.xml 中只有一个配置文件.在应用程序中,我有一个特定于配置文件的应用程序属性文件,如下所示:

So there is only one profile in settings.xml. In the application I have a profile-specific application properties file like that:

application-myprofile.properties :

spring.datasource.url=@db.url@
spring.datasource.username=@db.user@
spring.datasource.password=@db.password@

并且当我使用特定的配置文件 myprofile 启动 Spring Boot 应用程序时 - 一切正常,正在从 settings.xml 中提取凭据.

and while I am starting the spring boot application with a specific profile myprofile - it all works just fine, creds are being pulled from the settings.xml.

但是,对于测试,我的设置略有不同.

However, for the tests I have a slightly different setup.

我有一个这样的测试类:

I have a test class like that:

@SpringBootTest
@ActiveProfiles("mytestprofile")
public class MyTest {
    ....
}

test/resources 中相应的测试配置文件属性文件

and a corresponding test profile properties file inside test/resources like that

application-mytestprofile.properties

spring.datasource.url=@db.url@
spring.datasource.username=@db.user@
spring.datasource.password=@db.password@

在这种情况下,占位符不会被 settings.xml 中的值替换.但是,如果我将 application-mytestprofile.properties 的内容替换为

and in this case the placeholders are not being replaced by the values from settings.xml. However, if I replace the content of application-mytestprofile.properties with

spring.datasource.url=my_db_connection_url
spring.datasource.username=MyLogin
spring.datasource.password=MyPassword

它工作得很好.另外,如果我将占位符放在 application.properties 中:

it works just fine. Also, if I put the placeholders inside the application.properties:

spring.datasource.url=@db.url@
spring.datasource.username=@db.user@
spring.datasource.password=@db.password@

它也有效.

问题是:为什么会这样?如何在测试中使特定于配置文件的应用程序属性文件与 settings.xml 一起使用?

The question is: why is that? How can I make a profile-specific application properties file work with settings.xml in tests?

推荐答案

我没有关于您的项目设置的所有详细信息,但您的测试文件夹结构看起来像这样:

I don't have all the details about your project setup, but it looks like you have a test folder structure like this:

test 
    -> java 
            -> resources

虽然我认为应该是

test 
    -> java
    -> resources

因此,将您的测试资源文件夹向上移动一级,它应该可以工作.

So, move your test resources folder one level up, and it should work.

这篇关于替换特定于弹簧配置文件的属性文件中的属性占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆