Spring Boot的@ConfigurationProperties
注释有可能具有不可变(最终)字段吗?下面的例子不可变@ConfigurationProperties
@ConfigurationProperties(prefix = "example")
public final class MyProps {
private final String neededProperty;
public MyProps(String neededProperty) {
this.neededProperty = neededProperty;
}
public String getNeededProperty() { .. }
}
途径我试过到目前为止:
- 创建
MyProps
类的@Bean
有两个构造- 提供了两个构造函数:空与
neededProperty
参数 - 的豆是用
new MyProps()
创建的 - 在该领域的结果是
null
- 提供了两个构造函数:空与
- 使用
@ComponentScan
和@Component
提供MyProps
豆。- 结果
BeanInstantiationException
- >NoSuchMethodException: MyProps.<init>()
- 结果
我有它的工作的唯一方法是通过为每个非最终场提供的getter/setter。
据我所知,你是什么试图做的事情不会开箱即用。 – geoand 2014-10-01 10:17:31
很伤心。当然,我总是可以通过使用带'@ Value'注解的构造函数参数来实现纯Spring。但是,如果Spring Boot也支持这一点,那就太好了。 – RJo 2014-10-01 11:05:33
我在源代码上花了一个小小的高峰,但它并不平凡,以支持你所要求的东西。当然,我不是Spring内部专家,所以我可能会错过一些明显的东西 – geoand 2014-10-01 12:22:52