不可变的@ConfigurationProperties [英] Immutable @ConfigurationProperties

查看:326
本文介绍了不可变的@ConfigurationProperties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用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:


  1. 创建 @Bean MyProps 类的$ c>

    • 提供两个构造函数:empty和 neededProperty 参数

    • 使用创建新的MyProps()

    • 字段中的结果 null

  1. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆