如何使用 @SpringBootApplication Annotation 自动检测带有 @ConfigurationProperties 注释的类 [英] How is @ConfigurationProperties-annotated classes detected automatically with @SpringBootApplication Annotation

查看:44
本文介绍了如何使用 @SpringBootApplication Annotation 自动检测带有 @ConfigurationProperties 注释的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Spring Boot 并且有一个问题,其中包含参考文档中的一个示例.文档 提及

I am learning Spring Boot and have a question with one example in the reference documentation. Following section of the documentation mentions

6.使用@SpringBootApplication 注解

单个@SpringBootApplication 注释可用于启用这些三个特点,即:

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

@EnableAutoConfiguration: 启用 Spring Boot 的自动配置机制

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

@ComponentScan: 在包上启用@Component 扫描应用程序所在位置(请参阅最佳做法)

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

@Configuration: 允许在上下文中注册额外的 bean 或导入额外的配置类

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

和下面的示例用它启用的任何功能替换这个单个注释,这让我有点困惑.例子

and the following example to replace this single annotation by any of the features that it enables is bit confusing for me . The example

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);
    }

}

示例说明

在这个例子中,Application 就像任何其他 Spring Boot应用程序,除了@Component 注释的类和未检测到@ConfigurationProperties 注释的类自动,并且显式导入用户定义的 bean(请参阅@导入).

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 answer(Stephane Nicoll 2017 年 5 月 5 日 11:07)的评论部分中看到@Component官方不推荐使用注解来自动检测@ConfigurationProperties.所以我的假设是带有 @ConfigurationProperties 的 Spring 框架类没有使用 @Component 进行注释.

The only major difference I see in the example code above is that it does not have @ComponentScan annotation. I also read in the comments section of an SO answer (Stephane Nicoll May 5 '17 at 11:07) that @Component annotation is not recommended officially to auto detect @ConfigurationProperties. So my assumption is that Spring framework classes with @ConfigurationProperties are not annotated with @Component.

我还检查了 @SpringBootApplication 注释源,但无法识别任何可以自动检测 @ConfigurationProperties 注释类的内容.

Also I checked the @SpringBootApplication annotation source and couldn't identify anything that should enable the automatic detection of @ConfigurationProperties annotated classes.

参考文档2.8.3.启用@ConfigurationProperties 注释类型 部分显示了以下扫描和自动检测@ConfigurationProperties 的方法

The reference document 2.8.3. Enabling @ConfigurationProperties-annotated types section shows the following way to scan and autodetect @ConfigurationProperties

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

有了这些细节,我想了解

With all these details , I would like to understand

为什么在这个例子中明确提到 @ConfigurationProperties 注释的类不会自动检测?以及如何在使用@SpringBootApplication 时自动检测@ConfigurationProperties 注释类.

Why is it explicitly mentioned for this example that @ConfigurationProperties-annotated classes are not detected automatically ? and How is @ConfigurationProperties annotated classes automatically detected when @SpringBootApplication is used.

附加说明:我看到了 之前版本 文档和 current 一个.以下参考缺少当前的

Additional note : I saw a small difference between the prior version of the documentation and the current one. The following reference is missing the current one

请记住,@EnableConfigurationProperties 注释是还自动应用于您的项目,以便任何现有的 bean用@ConfigurationProperties 注释是从环境

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

推荐答案

以下是我从我的分析中了解到的.

Following is what I understand from my analysis.

@ConfigurationProperties 注释类型可以通过

  1. 使用 @ConfigurationProperties 注释类@ComponentScan 范围内的注解(@Component@Service 等).根据 Stephane Nicoll 的评论,不推荐,这对我来说很有意义.

  1. Annotating the class with @ConfigurationProperties with an annotation that falls in the scope of @ComponentScan ( @Component, @Service and the like ) . Not recommended as per the comment from Stephane Nicoll , which makes sense to me now.

使用注解@EnableConfigurationProperties .为此使用 @EnableConfigurationProperties 注释的类应该使用属于范围的注释进行注释@ComponentScan(@Component@Service 等)

Using annotation @EnableConfigurationProperties . For this to work the class annotated with @EnableConfigurationProperties should be annotated with an annotation that falls in the scope of @ComponentScan ( @Component, @Service and the like )

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

Using annotation @ConfigurationPropertiesScan and by making sure the classes annotated with @ConfigurationProperties is placed under its radar. The usage is similar to @ComponentScan .

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

Now , when we replace @SpringBootApplication with individual annotations that it enables and omit @ComponentScan (as in example) , the @EnableConfigurationProperties way (Point 2) of registering the types with @ConfigurationProperties will not work. This probably answers both my questions on why and how .

明确提到这一点可能是因为 @EnableConfigurationProperties@ComponentScan 之间的联系对于像我这样的人来说并不那么明显.

This was explicitly mentioned probably because the connection between @EnableConfigurationProperties and @ComponentScan is not that obvious for people like me.

其他详细信息.

当我们使用@EnableConfigurationProperties时,@ConfigurationProperties注解类型的注册是通过注解导入的EnableConfigurationPropertiesRegistrar类实现的

The registration of @ConfigurationProperties annotated types when we use @EnableConfigurationProperties happens through of the EnableConfigurationPropertiesRegistrar class that is imported by the annotation

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EnableConfigurationPropertiesRegistrar.class)
public @interface EnableConfigurationProperties {..}

这篇关于如何使用 @SpringBootApplication Annotation 自动检测带有 @ConfigurationProperties 注释的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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