Spring Boot 使用默认值而不是来自 application-local.yml 的值。

huangapple go评论65阅读模式
英文:

Spring boot is taking the default value instead of from application-local.yml

问题

我有一个类,它从配置文件中获取一个值,但它不起作用。

@Component
public class InputValidator {

private final Logger log = ESAPI.getLogger(this.getClass());

@Value("${apis.maxNumOfCics : 88}")
private Integer maxNumOfCics;

// 一些方法
}


我已经配置了一个随机的默认值88,尽管我在application-local.yml中将其配置为20,但它仍然被使用。当我不使用默认值时:

@Value("${apis.maxNumOfCics}")


它会从application-local.yml中获取正确的值,而且我也将活动配置文件设置为local。拼写是正确的,其他配置值都来自yml文件,但当我设置默认值时,它总是采用默认值。对此有任何帮助将不胜感激!
英文:

I have a class which takes a value from the config file but it is not working.

@Component
public class InputValidator {

    private final Logger log = ESAPI.getLogger(this.getClass());

    @Value("${apis.maxNumOfCics : 88}")
    private Integer maxNumOfCics;

// some methods
}

I have configured a random default value of 88 which is being used even though I configured it as 20 in application-local.yml. When i don't use a default value:

@Value("${apis.maxNumOfCics}")

it takes the correct value from application-local.yml, and I set the the active profile to local as well. The spelling is correct and the other config values are coming from the yml file but when I put a default it always takes the default. Any help on this would be appreciated!

答案1

得分: 1

I think the problem is the spaces in the expression. Try removing the spaces in your expression before and after the :.

@Value("${apis.maxNumOfCics:88}")
private Integer maxNumOfCics;

尝试移除表达式中冒号(:)前后的空格。

尝试使用以下语法之一:https://www.baeldung.com/spring-value-annotation

英文:

I think the problem is the spaces in the expression. Try removing the spaces in your expression before and after the :.

@Value("${apis.maxNumOfCics:88}")
private Integer maxNumOfCics;

Try one of the pieces of syntax from https://www.baeldung.com/spring-value-annotation

huangapple
  • 本文由 发表于 2023年4月4日 05:48:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定