[英]Which application.yml that're in src/main/resources and src/test/resources directories will be used when testing
In a spring boot Maven project, there's a application.yml
file in src/main/resources
and src/test/resources
respectively. 在Spring Boot Maven项目中,分别在
src/main/resources
和src/test/resources
有一个application.yml
文件。
When running test cases, it seems that testing will only use the file in src/test/resources
directory.It won't load two files and override the properties in src/main/resources/application.yml
. 运行测试用例时,似乎测试只会使用
src/test/resources
目录中的文件,它将不会加载两个文件并覆盖src/main/resources/application.yml
的属性。 And if there isn't the file in src/test/resources
directory, it will use the file in src/main/resources
directory. 如果
src/test/resources
目录中没有该文件,它将使用src/main/resources
目录中的文件。
How does spring boot to load the application.yml
file in these two directories? Spring Boot如何在这两个目录中加载
application.yml
文件? What're the rules? 有什么规定? or What does this action depend on?
或此动作取决于什么?
Where can I look up more information about this online? 在哪里可以在线查找有关此信息的更多信息?
└───spring-boot-main-test-application-yaml
├───pom.xml
└───src
├───main
│ ├───java
│ ├───resources
│ ├───application.yml
│
├───test
├───java
├───resources
└─── application.yml
src/main/resource/application.yml
name: Phil(main)
sample:
name: Andy(main)
src/test/resource/application.yml
name: Phil(test)
sample:
name: Andy(test)
case1: src/main/resource/application.yml
exists 情况1:
src/main/resource/application.yml
存在
output: Hello Phil(main),Andy(main) 输出:Hello Phil(main),Andy(main)
case2: src/main/resource/application.yml
and src/test/resource/application.yml
both exist 情况2:
src/main/resource/application.yml
和src/test/resource/application.yml
都存在
output: Hello Phil(test),Andy(test) 输出:Hello Phil(测试),Andy(测试)
case3: src/main/resource/application.yml
and src/test/resource/application.yml
both exist and comment out name
in src/test/resource/application.yml
情况3:
src/main/resource/application.yml
和src/test/resource/application.yml
都存在,并在src/test/resource/application.yml
注释掉name
output: 输出:
Error - java.lang.IllegalArgumentException: Could not resolve placeholder 'name' in value "${name}"
It seems that this test didn't read properties from src/main/resource/application.yml
so it couldn't find out name
property. 似乎此测试未从
src/main/resource/application.yml
读取属性,因此无法找到name
属性。
Spring detects those two directories and like you said it's figuring out which configs to use based on presence and priority. Spring会检测到这两个目录,就像您说的那样,它会根据状态和优先级确定要使用的配置。 If you wanted spring to use a particular setting you should define
spring profiles
which can have specialized settings as well as extend default settings. 如果希望弹簧使用特定设置,则应定义
spring profiles
,该spring profiles
可以具有专门的设置以及扩展的默认设置。
I am working with yml file and make sure that these property files are loaded when the app is build, it is preferred to have the following config in pom.xml 我正在使用yml文件,并确保在构建应用程序时加载了这些属性文件,因此在pom.xml中最好具有以下配置
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.jks</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
....
</build>
Make sure to run the build (if required start from scratch like deleting the repository folder inside .m2 - This might not be needed at all) Now check the target/classes
and that should contain the files of all the specified types 确保运行构建(如果需要,请从头开始,例如删除.m2中的存储库文件夹-可能根本不需要)。现在检查
target/classes
,其中应包含所有指定类型的文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.