【问题标题】:spring boot application context was not properly loaded if the yaml file wasn't application.yml如果 yaml 文件不是 application.yml,则未正确加载 spring boot 应用程序上下文
【发布时间】:2016-03-07 10:58:14
【问题描述】:

通过以下配置,我的测试可以正确读取yaml文件中的属性。

@SpringBootApplication
@PropertySource("classpath:application.yml")
@ComponentScan({ "com.my.service" })
public class MyApplication {

}

然后我将yaml文件重命名为my-application.yml,并将PropertySource改为

@PropertySource("classpath:my-application.yml")

由于 null 属性值,测试失败。配置类如下:

@Configuration
@ConfigurationProperties(prefix="my")
@Data
public class MyConfig {
    private String attr1;
}

测试类是:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
public class MyConfigTest {

@Autowired
private MyConfig myConfig;

@Test
public void getMyConfigTest() {
    Assert.assertNotNull(myConfig.getAttr1());
}

为什么spring boot能找到重命名后的yaml文件,却无法正确加载值?

【问题讨论】:

  • 你能发布属性文件的内容吗?

标签: spring-boot


【解决方案1】:

YAML files can’t be loaded via the @PropertySource annotation

它似乎可以与 @PropertySource("classpath:application.yml") 一起使用,因为这是默认位置,并且 Spring Boot 无论如何都会出现在那里。

您也许可以改用 @ConfigurationProperties(location="claspath:my-application.yml") ,但它并不能真正达到相同的目的(我自己从未尝试过)。

【讨论】:

  • 感谢您的回答。顺便说一句,我试过 @ConfigurationProperties(location="claspath:my-application.yml") ,它不起作用。
猜你喜欢
  • 1970-01-01
  • 2019-06-16
  • 2015-02-19
  • 1970-01-01
  • 2019-11-23
  • 2014-08-18
  • 1970-01-01
  • 2021-11-15
  • 2019-06-07
相关资源
最近更新 更多