@ConfigurationProperties-annotated 类 如何使用@SpringBootApplication Annotation 自动检测

How is @ConfigurationProperties-annotated classes detected automatically with @SpringBootApplication Annotation

我正在学习 Spring Boot 并对参考文档中的一个示例有疑问。 documentation 的以下部分提到

6.使用@SpringBootApplication 注释

A single @SpringBootApplication annotation can be used to enable those three features, that is:

@EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism

@ComponentScan: enable @Component scan on the package where the application is located (see the best practices)

@Configuration: allow to register extra beans in the context or import additional configuration classes

下面的示例用它启用的任何功能替换此单个注释让我有点困惑。示例

package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
@Import({ MyConfig.class, MyAnotherConfig.class })
public class Application {

    public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
    }

}

示例说明

In this example, Application is just like any other Spring Boot application except that @Component-annotated classes and @ConfigurationProperties-annotated classes are not detected automatically and the user-defined beans are imported explicitly (see @Import).

我在上面的示例代码中看到的唯一主要区别是它没有 @ComponentScan 注释。我还在 SO (Stephane Nicoll,2017 年 5 月 5 日,11:07)的评论部分中读到,官方不建议使用 @Component 注释来自动检测 @ConfigurationProperties。所以我的假设是 Spring framework 类 with @ConfigurationProperties 没有用 @Component.

注释

我还检查了 @SpringBootApplication 注释源,但无法识别任何应该启用自动检测 @ConfigurationProperties 注释 类 的内容。

参考文档2.8.3. Enabling @ConfigurationProperties-annotated types部分显示了以下扫描和自动检测@ConfigurationProperties

的方法
@SpringBootApplication
@ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" })
public class MyApplication {
}

有了这些细节,我想明白了

为什么在此示例中明确提到 @ConfigurationProperties-annotated 类 未被自动检测?以及如何在使用@SpringBootApplication 时自动检测@ConfigurationProperties 注释类。

补充说明:我发现 prior version of the documentation and the current 两者之间存在细微差别。以下参考缺少当前参考

Keep in mind that the @EnableConfigurationProperties annotation is also automatically applied to your project so that any existing bean annotated with @ConfigurationProperties is configured from the Environment

以下是我分析后的理解。

@ConfigurationProperties 注释类型可以通过

注册到 ApplicationContext
  1. @ConfigurationProperties 注释 class 落在 @ComponentScan 范围内的注解 ( @Component@Service 等)。根据 Stephane Nicoll 的评论不推荐,这对我来说很有意义。

  2. 使用注解 @EnableConfigurationProperties。为此 使用 @EnableConfigurationProperties 注释的 class 应该使用属于以下范围的注释进行注释 @ComponentScan@Component@Service 等)

  3. 使用注释 @ConfigurationPropertiesScan 并确保 用 @ConfigurationProperties 注释的 classes 被放置 在它的雷达下。用法类似于 @ComponentScan .

现在,当我们将 @SpringBootApplication 替换为它启用并省略 @ComponentScan(如示例)的单个注释时,@EnableConfigurationProperties 注册类型的方式(第 2 点) @ConfigurationProperties 将不起作用。这可能回答了我关于为什么和如何的问题。

这被明确提及可能是因为 @EnableConfigurationProperties@ComponentScan 之间的联系对像我这样的人来说并不那么明显。

其他详细信息。

我们使用@EnableConfigurationProperties@ConfigurationProperties注解类型的注册是通过注解

导入的EnableConfigurationPropertiesRegistrarclass发生的
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EnableConfigurationPropertiesRegistrar.class)
public @interface EnableConfigurationProperties {..}