繁体   English   中英

Spring Boot 未从 YML 文件加载属性

[英]Spring Boot is not loading the properties from a YML file

我的测试文件是:

@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MyApplicationTest {
    @InjectMocks
    @Autowired
    protected MyController myController;

} 

我的Controller文件是:

@RestController
@RequestMapping(value="${app.base-path}")  // Unable to load this property
public class MyController {

}

我试过@TestPropertySource但没有用。 SpringBootApplication工作正常。 @Value注释不是这种情况。 @Value注释装饰的字段工作得很好。

您是否将 .yml 文件命名为application.yml并将其放在src/main/resourcessrc/test/resources

将 application.yml 文件放在这些文件夹中会自动将其添加到类路径中,以便您可以在运行时访问此文件属性。

默认情况下,Spring Boot 将尝试加载 classpath 上可用的application.ymlapplication-{profilename}.yml ,因此您可以尝试使用application-test.yml (它将消除在一个文件中定义配置文件的问题 - 也许它会导致你的问题)。

此外,您可以尝试使用bootstrap.yml ,它在application.yml之前加载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM