spring boot配置属性错误

时间:2015-11-25 00:34:01

标签: java spring configuration spring-boot

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

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

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

1 个答案:

答案 0 :(得分:0)

要清除空气,@ ConsfigurationProperties与@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