简体   繁体   English

Spring 开机。 使用@ConfigurationProperties 注解的好处

[英]Spring boot. The profit of using @ConfigurationProperties annotation

Could you explain me the profit of using @ConfigurationProperties annotation.你能解释一下使用@ConfigurationProperties 注释的好处吗?

I have to options of my code.我必须选择我的代码。

First with @ConfigurationProperties annotation:首先使用 @ConfigurationProperties 注释:

@Service
@ConfigurationProperties(prefix="myApp")
public class myService() {
  private int myProperty;
  public vois setMyProperty(int myProperty) {
    this.myProperty = myProperty;
  }
  // And this I can use myProperty which was injected from application.properties (myApp.myProperty = 10)
}

The second one without @ConfigurationProperties annotation.第二个没有@ConfigurationProperties 注释。

It looks like this:它看起来像这样:

@Service
public class myService() {
    private final Environment environment;
    public myService(Environment environment) {
        this.environment = environment;
    }
  // And this I can use myProperty which was injected from application.properties (myApp.myProperty = 10)
  environment.getProperty("myApp.myProperty")
}

With one property the amount of code looks the same, but if I have about 10 properties, the first options will have more boilerplate of code (define 10 properties and 10 setters for this properties).使用一个属性,代码量看起来是一样的,但如果我有大约 10 个属性,第一个选项将有更多的代码样板(为此属性定义 10 个属性和 10 个设置器)。

The second options will have the one injection of Environment and no addition boilerplate code.第二个选项将注入一次 Environment 并且不添加样板代码。

@ConfigurationProperties is used when you want to map a set of properties from properties file with a Class properties. @ConfigurationProperties用于 map 属性文件中的一组属性 Class 属性。 Using @ConfigurationProperties make your code more readable, categorized / modular and cleaner.使用@ConfigurationProperties使您的代码更具可读性、分类/模块化和更清晰。 How?如何?

  1. You have application properties mapped to a POJO bean & ensures re-usability.您将应用程序属性映射到 POJO bean 并确保可重用性。

  2. You are using the spring bean with abstraction (properties are automatically injected).您正在使用带有抽象的 spring bean(属性会自动注入)。

Now if you use Environment bean, you will always have to call getProperty and then specify the string name of the property, so lots of boilerplate code.现在,如果您使用Environment bean,您将始终必须调用getProperty然后指定属性的字符串名称,因此需要大量样板代码。 Also, if you have to refactor some property and rename it you have to do it in all places it's being used in.此外,如果您必须重构某些属性并重命名它,则必须在所有使用它的地方进行。

Hence, my recommendation is when you have to group properties and re-use in multiple places go for @ConfigurationProperties .因此,我的建议是当您必须对属性进行分组并在多个地方重用@ConfigurationProperties时,@ConfigurationProperties。 If you have to use a property or two and only in one place throughout application then Environment can be used.如果您必须在整个应用程序中使用一个或两个属性并且仅在一个位置使用,则可以使用 Environment。

@ConfigurationProperties is annotation for externalized configuration. @ConfigurationProperties是外部化配置的注解。 To inject property value from a property file to a class, we can add @ConfigurationProperties at a class level要将属性文件中的属性值注入 class,我们可以在 class 级别添加@ConfigurationProperties

When using ConfigurationProperties you don't need to remember the property name to retrieve it.使用ConfigurationProperties时,您无需记住属性名称即可检索它。 Spring takes care of the mapping. Spring 负责映射。 For eg:例如:

app.properties.username-max-length in properties属性中app.properties.username-max-length

will be mapped to将映射到

String usernameMaxLength in POJO POJO 中的String usernameMaxLength

You just need to get usernameMaxLength field name right, once.您只需要一次正确获取usernameMaxLength字段名称。

Using ConfigurationProperties makes your code easy to test.使用ConfigurationProperties使您的代码易于测试。 You can have multiple properties file for different test scenarios and can use them in your tests with TestPropertySource("path") .您可以为不同的测试场景拥有多个属性文件,并可以在测试中使用它们TestPropertySource("path")

Now, if boilerplate getters / setters bothers you.现在,如果样板getters / setters困扰你。 You can always use lombok's @Getter / @Setter .你总是可以使用@Getter@Getter / @Setter This is just a suggestion and depends on an individual's choice.这只是一个建议,取决于个人的选择。

Also, starting from Spring Boot 2.2.0 , your @ConfigurationProperties classes can be immutable此外,从Spring Boot 2.2.0开始,您的@ConfigurationProperties类可以是不可变的

@ConfigurationProperties(prefix = "app.properties")
public class AppProperties {

    private final Integer usernameMaxLength;

    @ConstructorBinding
    public AppProperties(Integer usernameMaxLength) {

        this.usernameMaxLength = usernameMaxLength;
    }

    // Getters
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM