简体   繁体   English

Spring Boot 未从 YML 文件加载属性

[英]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/resourcessrc/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.ymlapplication-{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.

相关问题 "'application.yml' 中的 Spring Boot 属性未从 JUnit 测试加载" - Spring Boot properties in 'application.yml' not loading from JUnit Test 如何在Spring Boot应用程序中使用配置(properties / yml)文件中的属性? - How can I use properties from a configuration (properties/yml) file in my Spring Boot application? 在春季启动中从文件系统加载properties.yaml文件 - Loading properties.yaml files from file system in spring boot 从 spring 启动应用程序中的 properties.yml 文件中读取复数值 - Read complex values from properties.yml file in spring boot application 如何在Spring Boot应用程序中从YML文件加载多个属性 - How to load multiple properties from YML file in spring-boot application 无法从spring-boot中的Properties文件和Bootstrap.yml中选择值 - Unable to pick the values from Properties file and Bootstrap.yml in spring-boot 如何从 spring boot jdbc 存储库中的属性或 yml 文件存储和读取 SQL 查询? - How to store and read SQL queries from properties or yml file in spring boot jdbc repository? Spring Boot:外部属性文件未加载 - Spring Boot: External Properties File not loading 根据application.yml的属性注入Spring Boot - Inject with Spring Boot depending on properties from application.yml Spring Boot - 从依赖的 jar 加载 application.properties/yml - Spring Boot - Load application.properties/yml from dependent jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM