下面是我的部分堆栈跟踪:
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时,main是在build/classes/main中创建的,而不是在build/classes/test中创建的。
有人能帮我破例吗?
发布于 2015-11-25 00:57:40
若要将@ConfigurationProperties与@ConfigurationProperties结合起来,需要在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
https://stackoverflow.com/questions/33906515
复制相似问题