【问题标题】:@Value("${local.server.port}") not working in Spring boot 1.5@Value("${local.server.port}") 在 Spring Boot 1.5 中不起作用
【发布时间】:2017-09-14 02:10:53
【问题描述】:

我正在将 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 Spring Boot Test 也可以正常工作。

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

我是否遗漏了为此工作需要做的任何更改。

【问题讨论】:

  • 我尝试使用Environment 获取端口,但它返回-1
  • @MeenaChaudhary 你能发布 application.properties 即你在哪里指定这个属性?
  • 正如从 1.3 到 1.4 的迁移指南(您已跳过)中所述,在您的字段中使用 @LocalServerPort 而不是 @Value

标签: java spring spring-boot spring-boot-test


【解决方案1】:

您必须为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

【讨论】:

    【解决方案2】:

    添加我在其他地方拥有的另一个替代解决方案。

    我已经配置了

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
    public class YourTest {
    
      @LocalServerPort           // shorthand for @Value("${local.server.port}")
      private Integer port;
      ...
    }
    

    认为就是这样,即使在指定网络环境等时仍然出现此错误。我的${local.server.port} 似乎总是为空。

    一段时间后,我注意到我的 Spring Boot 启动消息不包含它正在使用的端口的概念,因此显然它根本没有监听任何端口 - 这解释了为什么它首先为 null。添加实际的容器实现依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
    

    导致这出现在我的日志中:

    2019-02-26 18:45:47.231  INFO 12504 --- [           main] o.s.b.web.embedded.jetty.JettyWebServer  : Jetty started on port(s) 43132 (http/1.1) with context path '/'
    

    之后 local.server.port 和 @LocalServerPort 也可以工作。

    【讨论】:

      【解决方案3】:

      对我来说,问题是在我的其他测试中有替代 @Configuration 类,如下所示:

      @Configuration
      public class ReadPropertiesConfiguration {
          @Bean
          PropertyPlaceholderConfigurer propConfig() {
              PropertyPlaceholderConfigurer placeholderConfigurer = new PropertyPlaceholderConfigurer();
              placeholderConfigurer.setLocation(new ClassPathResource("application.properties"));
              return placeholderConfigurer;
          }
      }
      

      并且应用程序的@SpringBootApplication 由于它的@ComponentScan 正在接收它,并且由于某种原因导致了这个问题。当为这些添加排除项和/或用其他解决方案替换它们时,事情又开始正常工作了。

      我不知道发生这种情况的根本原因,但这也可能是您的问题。

      【讨论】:

        【解决方案4】:

        首先确保属性在属性文件中的拼写正确。正如我几天前使用 spring-boot-1.5.2 所做的那样,它可以工作。

        或者

        你需要添加

        @PropertySource("classpath:application.properties")

        到你的班级,所以它会选择你的配置。

        如果您需要不同的配置进行测试,您可以添加

        @TestPropertySource(locations="classpath:test.properties")

        参考@Value not work on Spring Boot Test

        【讨论】:

        • 代码本身没有问题,因为它在 spring boot 1.3.8 上运行良好。
        • 好吧,可能是。你也可以发布一些代码类吗?看看是什么问题可能会有所帮助。
        • 你在使用基于 maven 的项目吗?
        猜你喜欢
        • 2018-09-04
        • 2018-03-03
        • 2018-04-03
        • 1970-01-01
        • 2019-05-30
        • 1970-01-01
        • 2018-09-24
        • 2020-10-31
        • 2019-11-25
        相关资源
        最近更新 更多