Spring @Value解析不正确[重复]

问题描述 投票:1回答:4

我有一个依赖项jar,其值在yaml中配置如下:

app:
  settings:
    value1 : true

Java代码为

public class LoadConfig {

    @Value("${app.settings.value1}")
    private Boolean value1;
}

在部署spring boot应用程序时,在运行时,未解析依赖项jar值,并且发生了以下问题。

org.springframework.beans.factory.UnsatisfiedDependencyException: 
    Error creating bean with name 'LoadConfig':
    Unsatisfied dependency expressed through field 'value1'; nested exception is 
    org.springframework.beans.TypeMismatchException:**Failed to convert value of type 'java.lang.String' to required type 'java.lang.Boolean'**; nested 
    exception is java.lang.IllegalArgumentException: Invalid boolean value [**${app.settings.value1}**].

问题不是值,而是将path(app.settings.value1)转换为布尔值并引发错误。

Maven项目结构为

MyJar --> app.jar
    |-> dependency.jar 
java spring spring-boot spring-annotations
4个回答
0
投票

基于this,您可以尝试:


-1
投票

也可以是原始的:


-1
投票

您可以尝试以下方式:


-1
投票

请尝试此代码:

© www.soinside.com 2019 - 2024. All rights reserved.