java - spring boot 配置属性错误

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

有人可以帮我解决这个异常(exception)吗?

最佳答案

为了消除误会,@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

关于java - spring boot 配置属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33906515/

相关文章:

java - 配置 Spring MVC Controller 以将文件发送到客户端

java - JAX-B 的多种配置

java - Rest 模板与根数组绑定(bind)

java - 用二维数组 : IndexOutOfBounds error 解决八皇后问题

java - 使用 Spring Rest Template + Spring Web MVC 上传多部分文件(多个文件)

java - 创建 ServletContext 资源中定义的名称为 'entityManagerFactory' 的 bean 时出错 [/WEB-INF/dispatcher-servlet.xml] :

configuration - Openproject:如何在docker环境中设置config/configuration.yml

hadoop - Hadoop 2.x 系列是否弃用了 `dfs.data.dir` 属性?

java - 在 WebLogic 上设置默认的 CookieManager 没有效果

java - 读取和删除文件中的某些行时遇到问题?