@Value("${local.server.port}") 在 Spring Boot 1.5 中不起作用 [英] @Value("${local.server.port}") not working in Spring boot 1.5

查看:22
本文介绍了@Value("${local.server.port}") 在 Spring Boot 1.5 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am upgrading Spring Boot from 1.3 to 1.5. For upgrading to 1.5 I have replaced

@SpringApplicationConfiguration(classes = TestConfig.class)@WebIntegrationTest

@SpringBootTest(classes = TestConfig.class)

另外,我正在使用

@Value("${local.server.port}")受保护的 int 端口;

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

to get port number defined in application.properties file. I further use this port number to build a REST URL.

但是在升级后,我收到以下错误,而 1.3 Spring Boot Test 也可以正常工作.

But after the upgrade I am getting the error below whereas the same works fine with 1.3 Spring Boot Test.

引起:java.lang.IllegalArgumentException:无法解析值${local.server.port}"中的占位符local.server.port"在 org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)

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)

我是否遗漏了我需要做的任何更改才能使其正常工作.

Am I missing any changes that I need to do for this to work.

推荐答案

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

You have to provide a value for webEnvironment. In your case DEFINED_PORT like this

@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-应用程序

这篇关于@Value("${local.server.port}") 在 Spring Boot 1.5 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆