Spring boot 如何使用@SpringBootApplication注释自动检测@ConfigurationProperties注释类
我正在学习SpringBoot,在参考文档中有一个例子,我有一个问题。 下面一节提到 6。使用@SpringBootApplication注释 可以使用单个@SpringBootApplication注释来启用这些功能 三个特点,即: @EnableAutoConfiguration:启用Spring引导的自动配置 机制 @ComponentScan:在安装组件的包上启用@Component扫描 应用程序位于(请参阅最佳实践) @配置:允许在上下文或导入中注册额外的bean 其他配置类 下面的例子将这个注释替换为它启用的任何功能,这让我有点困惑。榜样Spring boot 如何使用@SpringBootApplication注释自动检测@ConfigurationProperties注释类,spring-boot,component-scan,Spring Boot,Component Scan,我正在学习SpringBoot,在参考文档中有一个例子,我有一个问题。 下面一节提到 6。使用@SpringBootApplication注释 可以使用单个@SpringBootApplication注释来启用这些功能 三个特点,即: @EnableAutoConfiguration:启用Spring引导的自动配置 机制 @ComponentScan:在安装组件的包上启用@Component扫描 应用程序位于(请参阅最佳实践) @配置:允许在上下文或导入中注册额外的bean 其他配置类 下面的例
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);
}
}
示例说明
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);
}
}
在本例中,应用程序与任何其他Spring引导程序一样
除@Component注释类和
@ConfigurationProperties注释类未检测到
自动并显式导入用户定义的bean(请参见
@进口)
我在上面的示例代码中看到的唯一主要区别是它没有@ComponentScan
注释。我还在SO的评论部分(Stephane Nicoll 5月5日17日11:07)中读到,官方不建议@Component annotation自动检测@ConfigurationProperties。因此,我的假设是,带有@ConfigurationProperties的Spring框架类不带@Component注释
此外,我还检查了@SpringBootApplication
注释源,无法识别任何能够自动检测@ConfigurationProperties
注释类的内容
参考文档部分显示了以下扫描和自动检测@ConfigurationProperties的方法
@SpringBootApplication
@ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" })
public class MyApplication {
}
有了这些细节,我想了解一下
为什么在本例中明确提到@ConfigurationProperties注释类不会自动检测到?当使用@SpringBootApplication时,@ConfigurationProperties注释类是如何自动检测的
附加说明:我看到文档的格式与另一个有一点不同。以下引用缺少当前引用
请记住,@EnableConfigurationProperties注释是
还自动应用于您的项目,以便任何现有bean
使用@ConfigurationProperties进行注释是从
环境
以下是我从分析中了解到的情况
@ConfigurationProperties
注释类型可以通过以下方式注册到ApplicationContext
@ConfigurationProperties
和
属于@ComponentScan
(
@组件
,@服务
等)。根据史蒂芬·尼科尔的评论,不推荐使用,这对我现在来说是有意义的@EnableConfigurationProperties
。为了这个
使用@EnableConfigurationProperties
应使用属于以下范围的注释进行注释:
@组件扫描
(@组件
、@服务
等)@ConfigurationPropertiesScan
并确保
将放置带有@ConfigurationProperties
注释的类
在它的雷达下。其用法类似于@ComponentScan
@SpringBootApplication
替换为启用和省略@ComponentScan
(如示例所示)的单个注释时,@EnableConfigurationProperties
向@ConfigurationProperties
注册类型的方式(第2点)将不起作用。这可能回答了我关于为什么和如何的两个问题
这可能是因为@EnableConfigurationProperties
和@ComponentScan
之间的联系对于像我这样的人来说并不明显
其他详细信息。
当我们使用@EnableConfigurationProperties
时,通过注释导入的EnableConfigurationPropertiesRegistrar
类注册@ConfigurationProperties
注释类型
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EnableConfigurationPropertiesRegistrar.class)
public @interface EnableConfigurationProperties {..}