是否可以使用Spring Boot的 @ConfigurationProperties 注释获得不可变(最终)字段?以下示例
Is it possible to have immutable (final) fields with Spring Boot's @ConfigurationProperties annotation? Example below
@ConfigurationProperties(prefix = "example") public final class MyProps { private final String neededProperty; public MyProps(String neededProperty) { this.neededProperty = neededProperty; } public String getNeededProperty() { .. } }到目前为止我尝试过的方法:
Approaches I've tried so far:
- 提供两个构造函数:empty和 neededProperty 参数
- 使用创建新的MyProps()
- 字段中的结果 null
- Creating a @Bean of the MyProps class with two constructors
- Providing two constructors: empty and with neededProperty argument
- The bean is created with new MyProps()
- Results in the field being null
- 结果 BeanInstantiationException - > NoSuchMethodException:MyProps。< init>()
- Results in BeanInstantiationException -> NoSuchMethodException: MyProps.<init>()
我工作的唯一方法是为每个非最终字段提供getter / setter。
The only way I have got it working is by providing getter/setter for each non-final field.
推荐答案我必须经常解决这个问题而且我使用的有点不同方法,允许我在类中使用 final 变量。
I have to resolve that problem very often and I use a bit different approach, which allows me to use final variables in a class.
首先,我保留所有配置在一个地方(类),比如称为 ApplicationProperties 。该类具有 @ConfigurationProperties 具有特定前缀的注释。它也列在 @EnableConfigurationProperties 注释中,用于配置类(或主类)。
First of all, I keep all my configuration in a single place (class), say, called ApplicationProperties. That class has @ConfigurationProperties annotation with a specific prefix. It is also listed in @EnableConfigurationProperties annotation against configuration class (or main class).
然后我提供我的 ApplicationProperties 作为构造函数参数,并对构造函数中的 final 字段执行赋值。
Then I provide my ApplicationProperties as a constructor argument and perform assignment to a final field inside a constructor.
示例:
主要类:
@SpringBootApplication @EnableConfigurationProperties(ApplicationProperties.class) public class Application { public static void main(String... args) throws Exception { SpringApplication.run(Application.class, args); } }ApplicationProperties class
ApplicationProperties class
@ConfigurationProperties(prefix = "myapp") public class ApplicationProperties { private String someProperty; // ... other properties and getters public String getSomeProperty() { return someProperty; } }以及具有最终属性的类
@Service public class SomeImplementation implements SomeInterface { private final String someProperty; @Autowired public SomeImplementation(ApplicationProperties properties) { this.someProperty = properties.getSomeProperty(); } // ... other methods / properties }我更喜欢这种方法,原因很多,例如如果我必须在构造函数中设置更多属性,我的构造函数参数列表不是巨大,因为我总是有一个参数(在我的情况下 ApplicationProperties );如果需要添加更多 final 属性,我的构造函数保持不变(只有一个参数) - 这可能会减少其他地方的更改次数等。
I prefer this approach for many different reasons e.g. if I have to setup more properties in a constructor, my list of constructor arguments is not "huge" as I always have one argument (ApplicationProperties in my case); if there is a need to add more final properties, my constructor stays the same (only one argument) - that may reduce number of changes elsewhere etc.
我希望这会有所帮助
更多推荐
不可变的@ConfigurationProperties
发布评论