@Value 可以读取,但是@ConfigurationProperties 不能
@Value can reads, but @ConfigurationProperties can't
我正在尝试读取这样的 yml 文件。
order:
foo: 5000
bar: 12
我可以用 @value
阅读它。 (顺便说一句,我正在使用龙目岛)
@Component
@Data
public class WebConfigProperty {
private Integer foo;
private Integer bar;
public WebConfigProperty(@Value("${order.foo}") @NonNull final Integer foo,
@Value("${order.bar}") @NonNull final Integer bar) {
super();
this.foo = foo;
this.bar = bar;
}
}
我正在尝试使用 @ConfigurationProperties
,因为 yml 文件会变得更加复杂。但它不适用于 @ConfigurationProperties
.
@Component
@ConfigurationProperties("order")
@Data
public class WebConfigProperty {
@NonNull
private Integer foo;
@NonNull
private Integer bar;
}
我还在配置 class 中添加了 @EnableConfigurationProperties
。 config中的所有注解都是这样
@SpringBootConfiguration
@EnableConfigurationProperties
@EnableAutoConfiguration(exclude = { ... })
@ComponentScan(basePackages = { ... })
@Import({ ... })
@EnableCaching
错误信息是这样的
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in {...}.WebConfigProperty required a bean of type 'java.lang.Integer' that could not be found.
Action:
Consider defining a bean of type 'java.lang.Integer' in your configuration.
似乎 Spring 找不到 yml 文件并试图将空值放入 WebConfigProperty
字段。不知道为什么。
仅供参考,这是一个使用 Gradle 的多项目应用程序。 yml文件和一个配置class(没写)在同一个项目里。 WebConfigProperty
在另一个项目上。
编辑:
根据@Yannic Klem 的回答,这两个有效。
@Component
@ConfigurationProperties("order")
@Getter
@Setter
@EqualsAndHashCode
public class WebConfigProperty {
@NonNull
private Integer foo;
@NonNull
private Integer bar;
}
//OR
@Component
@ConfigurationProperties("order")
@Data
@NoArgsConstructor
public class WebConfigProperty {
@NonNull
private Integer foo;
@NonNull
private Integer bar;
}
Lomboks @Data
注释添加了一个 @RequiredArgsConstructor
。
Spring 然后尝试将参数自动装配到构造函数。
这会导致您的异常,因为它试图查找类型为 Integer
的两个 bean:foo 和 bar。
@ConfigurationProperties
的属性应该只有一个默认构造函数和 getters + setters。
然后,这些设置器将这些属性绑定到您的 @ConfigurationProperties
class。
您的 WebConfigProperty
可能如下所示:
@Component
@ConfigurationProperties("order")
/**
* Not sure about IDE support for autocompletion in application.properties but your
* code should work. Maybe just type those getters and setters yourself ;)
*/
@Getters
@Setters
public class WebConfigProperty {
@NonNull
private Integer foo;
@NonNull
private Integer bar;
}
我正在尝试读取这样的 yml 文件。
order:
foo: 5000
bar: 12
我可以用 @value
阅读它。 (顺便说一句,我正在使用龙目岛)
@Component
@Data
public class WebConfigProperty {
private Integer foo;
private Integer bar;
public WebConfigProperty(@Value("${order.foo}") @NonNull final Integer foo,
@Value("${order.bar}") @NonNull final Integer bar) {
super();
this.foo = foo;
this.bar = bar;
}
}
我正在尝试使用 @ConfigurationProperties
,因为 yml 文件会变得更加复杂。但它不适用于 @ConfigurationProperties
.
@Component
@ConfigurationProperties("order")
@Data
public class WebConfigProperty {
@NonNull
private Integer foo;
@NonNull
private Integer bar;
}
我还在配置 class 中添加了 @EnableConfigurationProperties
。 config中的所有注解都是这样
@SpringBootConfiguration
@EnableConfigurationProperties
@EnableAutoConfiguration(exclude = { ... })
@ComponentScan(basePackages = { ... })
@Import({ ... })
@EnableCaching
错误信息是这样的
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in {...}.WebConfigProperty required a bean of type 'java.lang.Integer' that could not be found.
Action:
Consider defining a bean of type 'java.lang.Integer' in your configuration.
似乎 Spring 找不到 yml 文件并试图将空值放入 WebConfigProperty
字段。不知道为什么。
仅供参考,这是一个使用 Gradle 的多项目应用程序。 yml文件和一个配置class(没写)在同一个项目里。 WebConfigProperty
在另一个项目上。
编辑: 根据@Yannic Klem 的回答,这两个有效。
@Component
@ConfigurationProperties("order")
@Getter
@Setter
@EqualsAndHashCode
public class WebConfigProperty {
@NonNull
private Integer foo;
@NonNull
private Integer bar;
}
//OR
@Component
@ConfigurationProperties("order")
@Data
@NoArgsConstructor
public class WebConfigProperty {
@NonNull
private Integer foo;
@NonNull
private Integer bar;
}
Lomboks @Data
注释添加了一个 @RequiredArgsConstructor
。
Spring 然后尝试将参数自动装配到构造函数。
这会导致您的异常,因为它试图查找类型为 Integer
的两个 bean:foo 和 bar。
@ConfigurationProperties
的属性应该只有一个默认构造函数和 getters + setters。
然后,这些设置器将这些属性绑定到您的 @ConfigurationProperties
class。
您的 WebConfigProperty
可能如下所示:
@Component
@ConfigurationProperties("order")
/**
* Not sure about IDE support for autocompletion in application.properties but your
* code should work. Maybe just type those getters and setters yourself ;)
*/
@Getters
@Setters
public class WebConfigProperty {
@NonNull
private Integer foo;
@NonNull
private Integer bar;
}