加载配置文件时如何修复空值错误

时间:2019-01-21 20:37:16

标签: spring-boot configuration-files

Spring Boot在自动配置组件时抛出Caused by: java.lang.IllegalArgumentException: null。我的null文件中没有application.properties属性。这是一个谜。

我尝试了一切。错误消息表明问题出在文件中的factory.core属性。我尝试将其删除,但出现了一个更通用的错误。如果我将factory.core放回原处,那么错误会专门抱怨它。

我的application.properties具有以下四个:

factory.protocol=http
factory.host=localhost
factory.core=recommend
factory.port=8983

我有这些注释

@Configuration
@ConfigurationProperties("factory")

我的代码只有这些字段的设置方法和获取方法。

private String protocol;
private String host;
private int port;
private String core;

Spring Boot将其写出来。

Description:

    Failed to bind properties under 'factory' to com.ot.cem.recommender.engine.ClientFactory$$EnhancerBySpringCGLIB$$1530d0ff:

        Property: factory.core
        Value: recommend
        Origin: class path resource [application.properties]:4:14
        Reason: Failed to bind properties under 'factory' to com.ot.cem.recommender.engine.ClientFactory$$EnhancerBySpringCGLIB$$1530d0ff

Action:

Update your application's configuration
The exception is
    Caused by: java.lang.IllegalArgumentException: null
        at java.lang.reflect.Array.newArray(Native Method) ~[na:1.8.0_162]
        at java.lang.reflect.Array.newInstance(Array.java:75) ~[na:1.8.0_162]
        at org.springframework.boot.context.properties.bind.Bindable.box(Bindable.java:255) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
        at org.springframework.boot.context.properties.bind.Bindable.of(Bindable.java:248) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]

3 个答案:

答案 0 :(得分:0)

您不需要@Configuration和@ConfigurationProperties。尝试删除@Configuration。 一些有用的链接:link1link2

答案 1 :(得分:0)

您是否在依赖项中包括配置注释处理器?

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

请参见https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor

那个或Spring确实找不到您的application.properties文件。您也可以通过注释每个字段来进行测试,例如@Value("factory.prototype")

答案 2 :(得分:0)

感谢您的答复。我尝试删除@Configuration,但结果相同。 link2在两个注释之间有很好的解释。我忘了在原始帖子中声明,我还设置了@Component批注,因为我可以自动装配要在另一个类中配置的类。

我使用Gradle,所以我有 编译(“ org.springframework.boot:spring-boot-configuration-processor:2.0.6.RELEASE”)。我阅读了文档链接。我认为Spring可以找到application.properties,因为错误提到了其中一个属性。就我而言,是factory.core。

不确定该怎么做。据我所确定,它应该可以工作。