Guide to @EnableConfigurationProperties – 指南:@EnableConfigurationProperties

最后修改: 2019年 6月 30日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this quick tutorial, we’ll show how to use an @EnableConfigurationProperties annotation with @ConfigurationProperties annotated classes.

在这个快速教程中,我们将展示如何在@ConfigurationProperties注解的类中使用@EnableConfigurationProperties注释。

2. Purpose of @EnableConfigurationProperties Annotation

2.@EnableConfigurationProperties注释的目的

@EnableConfigurationProperties annotation is strictly connected to @ConfiguratonProperties.

@EnableConfigurationProperties注解与@ConfiguratonProperties.严格相连。

It enables support for @ConfigurationProperties annotated classes in our application. However, it’s worth to point out that the Spring Boot documentation says, every project automatically includes @EnableConfigurationProperties. Therefore, @ConfiguratonProperties support is implicitly turned on in every Spring Boot application.

它使我们的应用程序支持@ConfigurationProperties注释的类。然而,值得指出的是,Spring Boot 文档说,每个项目都自动包括@EnableConfigurationProperties。因此,@ConfiguratonProperties支持在每个Spring Boot应用程序中都被隐含地打开了。

In order to use a configuration class in our project, we need to register it as a regular Spring bean.

为了在我们的项目中使用配置类,我们需要将它注册为一个普通的Spring Bean。

First of all, we can annotate such a class with @Component. Alternatively, we can use a @Bean factory method.

首先,我们可以用@Component.来注释这样一个类,或者,我们可以使用@Bean工厂方法。

However, in certain situations, we may prefer to keep a @ConfigurationProperties class as a simple POJO. This is when @EnableConfigurationProperties comes in handy. We can specify all configuration beans directly on this annotation.

然而,在某些情况下,我们可能更喜欢将@ConfigurationProperties类作为一个简单的POJO。这时@EnableConfigurationProperties就派上用场了。我们可以在这个注解上直接指定所有的配置Bean。

This is a convenient way to quickly register @ConfigurationProperties annotated beans.

这是一种快速注册@ConfigurationProperties注释的Bean的方便方法。

3. Using @EnableConfigurationProperties

3.使用@EnableConfigurationProperties

Now, let’s see how to use @EnableConfigurationProperties in practice.

现在,让我们看看如何在实践中使用@EnableConfigurationProperties

First, we need to define our example configuration class:

首先,我们需要定义我们的示例配置类。

@ConfigurationProperties(prefix = "additional")
public class AdditionalProperties {

    private String unit;
    private int max;

    // standard getters and setters
}

Note that we annotated the AdditionalProperties only with @ConfigurationProperties. It’s still a simple POJO!

注意,我们只用@ConfigurationProperties注释了AdditionalProperties这仍然是一个简单的POJO!

Finally, let’s register our configuration bean using @EnableConfigurationProperties:

最后,让我们使用@EnableConfigurationProperties注册我们的配置Bean。

@Configuration
@EnableConfigurationProperties(AdditionalProperties.class)
public class AdditionalConfiguration {
    
    @Autowired
    private AdditionalProperties additionalProperties;
    
    // make use of the bound properties
}

That’s all! We can now use AdditionalProperties like any other Spring bean.

这就是全部!我们现在可以像其他Spring Bean一样使用 AdditionalProperties

4. Conclusion

4.总结

In this quick tutorial, we presented a convenient way to quickly register a @ConfigurationProperties annotated class in Spring.

在这个快速教程中,我们介绍了在Spring中快速注册@ConfigurationProperties注解类的便捷方法。

As usual, all the examples used in this article are available over on GitHub.

像往常一样,本文中使用的所有例子都可以在GitHub上找到