【问题标题】:spring boot configurationproperties errorspring boot 配置属性错误
【发布时间】:2016-02-27 15:40:34
【问题描述】:

以下是我的部分堆栈跟踪:

    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 91 more
Caused by: org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 6 errors
Field error in object 'responsys' on field 'contactList': rejected value [null]; codes [NotNull.responsys.contactList,NotNull.contactList,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [responsys.contactList,contactList]; arguments []; default message [contactList]]; default message [may not be null]
Field error in object 'responsys' on field 'endpoint': rejected value [null]; codes [NotNull.responsys.endpoint,NotNull.endpoint,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [responsys.endpoint,endpoint]; arguments []; default message [endpoint]]; default message [may not be null]
Field error in object 'responsys' on field 'retryDelaysInSeconds': rejected value [null]; codes [NotNull.responsys.retryDelaysInSeconds,NotNull.retryDelaysInSeconds,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [responsys.retryDelaysInSeconds,retryDelaysInSeconds]; arguments []; default message [retryDelaysInSeconds]]; default message [may not be null]
Field error in object 'responsys' on field 'username': rejected value [null]; codes [NotNull.responsys.username,NotNull.username,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [responsys.username,username]; arguments []; default message [username]]; default message [may not be null]
Field error in object 'responsys' on field 'maxBatchSize': rejected value [0]; codes [Min.responsys.maxBatchSize,Min.maxBatchSize,Min.int,Min]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [responsys.maxBatchSize,maxBatchSize]; arguments []; default message [maxBatchSize],1]; default message [Minumum value is 1 and it disables batching completely]
Field error in object 'responsys' on field 'password': rejected value [null]; codes [NotNull.responsys.password,NotNull.password,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [responsys.password,password]; arguments []; default message [password]]; default message [may not be null]
    at org.springframework.boot.bind.PropertiesConfigurationFactory.validate(PropertiesConfigurationFactory.java:350)

以下是我的@ConfigurationProperties 类:

@Configuration
@ConfigurationProperties(prefix="responsys")
public class ResponsysConfig {

    @NotNull
    private String username;

    @NotNull
    private String password;

    @NotNull
    private String endpoint;

    @NotNull
    private String contactList;

    @NotNull
    private float expireTasksAfterHours;

    @NotNull
    @Max(value = 200, message = "Responsys API maximum batch size is 200")
    @Min(value = 1, message = "Minumum value is 1 and it disables batching completely")

    private int maxBatchSize;

    @NotNull
    private int batchAggregationTimeInMS;

    @NotNull
    private String retryDelaysInSeconds;


    private int[] retriesInSecondsInt;

    private String folderName;
    ....
    ....

以下是我的 application.properties 文件:

responsys.username=xxxx
responsys.password=xxxx
responsys.endpoint=xxxx
responsys.contactList=xxxx
responsys.retriesInSeconds=xxxx,xxx
responsys.expireTasksAfterHours=xx
responsys.maxBatchSize=xx
responsys.batchAggregationTimeInMS=xxxx

我在 src/main/resources 和 src/test/resources 中有相同的 application.properties 文件。但是,我的应用程序上下文加载在单元测试中失败,但在运行主应用程序时没有失败。

我还看到,当我执行gradle build 时,META-INF 是在 build/classes/main 中创建的,但它不是在 build/classes/test 中创建的。\

有人可以帮我解决这个例外吗?

【问题讨论】:

  • src/test/resource 最后没有s
  • @chrylis:这是一个错字。固定。

标签: java spring configuration spring-boot


【解决方案1】:

要清除 @ConfigurationProperties 和 @Configuration 的问题,需要在你的 bean 中添加 setter 和 getter,请添加它们,这个错误就会消失。

或者使用@Value 注释来减少代码噪音,例如:

@Configuration
public class ResponsysConfig {

    @NotNull
    @Value("${responsys.username}")
    private String username;

...


}

这里还有一些参考,仅供参考:

https://stackoverflow.com/questions/33795586/spring-boot-how-to-get-key-value-on-application-properties/33795916#33795916

http://www.javacodegeeks.com/2014/09/using-configurationproperties-in-spring-boot.html

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

【讨论】:

  • 我的班上确实有 setter 和 getter。而且我不想使用“@”Value注释,因为spring boot支持“@”ConfigurationProperties
猜你喜欢
  • 2019-05-19
  • 2020-06-14
  • 1970-01-01
  • 2017-06-24
  • 2020-05-02
  • 2021-05-10
  • 2014-09-19
  • 2019-04-21
  • 2014-01-18
相关资源
最近更新 更多