在没有Spring Boot Application的情况下阅读@PropertySource

时间:2018-03-19 06:55:52

标签: java spring spring-boot

我有一个具有以下结构的多模块Spring Boot项目:

  • MyAppModule
    • 的src /主/爪哇/ Application.java
    • ...
  • IntegrationTests
    • 的src /测试/ JAVA / integrationTest.java

IntegrationTest 模块正在测试 MyAppModule IntegrationTest 模块没有主包。因此没有Spring应用程序。它只有测试包。

尽管如此,我想在application.yaml中读取一些属性,但我无法做到,因为属性总是为null:

@Configuration
@PropertySource("classpath:application.yaml")
public class IntegrationTest {

  @Value("${app.url}")
  private String appUrl;

}

是否可以使用@Value注释而无需使用 Spring应用程序(主要使用SpringApplication.run等)?

3 个答案:

答案 0 :(得分:1)

为什么不只是@ConfigurationProperties?

@Configuration
@ConfigurationProperties(prefix = "app")
public class IntegrationTest {

 private String url;

 }

答案 1 :(得分:0)

在弹簧配置中添加用于组件扫描的集成测试基础包。

@ComponentScan("basePackage1,basePackage2....etc")

答案 2 :(得分:0)

感谢所有回复。使用以下代码解决了这个问题,并且必须使用SpringBootApplication。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class IntegrationTest {

 @Value("${app.url}")
 private String url;

 }
相关问题