[英]Spring Boot is not loading the properties from a YML file
My test file is :我的测试文件是:
@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MyApplicationTest {
@InjectMocks
@Autowired
protected MyController myController;
}
My Controller
file is:我的
Controller
文件是:
@RestController
@RequestMapping(value="${app.base-path}") // Unable to load this property
public class MyController {
}
I have tried @TestPropertySource
but did not work.我试过
@TestPropertySource
但没有用。 The SpringBootApplication
works fine. SpringBootApplication
工作正常。 This is not the case with @Value
annotation. @Value
注释不是这种情况。 Fields decorated with @Value
annotation works just fine.用
@Value
注释装饰的字段工作得很好。
Did you name your .yml file as application.yml
and put it on the src/main/resources
or src/test/resources
?您是否将 .yml 文件命名为
application.yml
并将其放在src/main/resources
或src/test/resources
?
Placing the application.yml file on those folders will automatically add it to the classpath so you can access this file properties at runtime.将 application.yml 文件放在这些文件夹中会自动将其添加到类路径中,以便您可以在运行时访问此文件属性。
By default, Spring Boot will try to load application.yml
and application-{profilename}.yml
available on classpath, so you could try with application-test.yml
(it will eliminate the problem with defining profiles in one file - maybe it causes your problem).默认情况下,Spring Boot 将尝试加载 classpath 上可用的
application.yml
和application-{profilename}.yml
,因此您可以尝试使用application-test.yml
(它将消除在一个文件中定义配置文件的问题 - 也许它会导致你的问题)。
Also, you could try with bootstrap.yml
, which is loaded before application.yml
.此外,您可以尝试使用
bootstrap.yml
,它在application.yml
之前加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.