编程语言
首页 > 编程语言> > java – @Value(“${local.server.port}”)无法在Spring boot 1.5中运行

java – @Value(“${local.server.port}”)无法在Spring boot 1.5中运行

作者:互联网

我正在将Spring Boot从1.3升级到1.5.升级到1.5我已经更换了

@SpringApplicationConfiguration(classes = TestConfig.class)
 @WebIntegrationTest

@SpringBootTest(classes = TestConfig.class)

另外,我正在使用

@Value( “${} local.server.port”)
 protected int port;

获取application.properties文件中定义的端口号.我进一步使用此端口号来构建REST URL.

但升级后我得到了下面的错误,而1.3弹簧启动测试同样正常.

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘local.server.port’ in value “${local.server.port}”
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)

我错过了为此工作需要做的任何更改.

解决方法:

您必须为webEnvironment提供值.在你的情况下DEFINED_PORT就像这样

@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class YourTest {

  @LocalServerPort           // shorthand for @Value("${local.server.port}")
  private Integer port;
  ...
}

有关详细信息,请参阅:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications

标签:spring-boot-test,java,spring,spring-boot
来源: https://codeday.me/bug/20190828/1752637.html