spring 引导配置属性错误

spring boot configurationproperties error

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

    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 class:

@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 中创建的。\

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

要清除@ConfigurationProperties 与@Configuration 的关系,您的bean 中需要setter 和getter,请添加它们,此错误就会消失。

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

@Configuration
public class ResponsysConfig {

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

...


}

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

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