spring boot 自动扫描Controller、Service、Component原理

项目里面为什么不加上@ComponentScan("com.yym.*")注解,也能加载到子目录里面的Controller,Service,Component的bean呢?

启动类没有@ComponentScan注解

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

原因:

spring boot 启动类加上@SpringBootApplication会自动扫描当前目录,及子目录下的Controller,Service,Component注解的bean。

查看@SpringBootApplication注解源码,里面有@ComponentScan注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication

查看Controller,Service注解源码都有Component注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {/*** The value may indicate a suggestion for a logical component name,* to be turned into a Spring bean in case of an autodetected component.* @return the suggested component name, if any (or empty String otherwise)*/@AliasFor(annotation = Component.class)String value() default "";}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {/*** The value may indicate a suggestion for a logical component name,* to be turned into a Spring bean in case of an autodetected component.* @return the suggested component name, if any (or empty String otherwise)*/@AliasFor(annotation = Component.class)String value() default "";}

原理:

##AnnotationConfigServletWebServerApplicationContext构造器初始化AnnotatedBeanDefinitionReader、ClassPathBeanDefinitionScanner

##AnnotatedBeanDefinitionReader构造器初始化调用AnnotationConfigUtils.registerAnnotationConfigProcessors静态方法注册ConfigurationClassPostProcessor.class

##注册ConfigurationClassPostProcessor.class的BeanDefinition

##解析启动配置类

##看到ConfigurationClassParser解析ComponentScans.class, ComponentScan.class注解

##ComponentScanAnnotationParser的parse方法解析

##包名为空,添加启动类所在的包

##找到启动类所在包,及子包所有的bean候选者

##至此类被扫描成BeanDefinition并注册到DefaultListableBeanFactory的beanDefinitionMap

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/336376.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

快乐学Python,数据分析之获取数据方法「公开数据或爬虫」

学习Python数据分析,第一步是先获取数据,为什么说数据获取是数据分析的第一步呢,显而易见:数据分析,得先有数据,才能分析。 作为个人来说,如何获取用于分析的数据集呢? 1、获取现成…

华为 1+X《网络系统建设与运维(高级)》认证模拟实验上机试题

华为 1X《网络系统建设与运维(高级)》认证模拟实验上机试题 一、考试背景二、考试说明2.1考试分数说明2.2考试要求2.3考试环境介绍2.4启动考试环境2.5保存答案(非常重要) 三、考试正文3.1注意事项3.2校区内(LAN)3.2.1任务 1&#…

【力扣每日一题】力扣2696删除子串后的字符串最小长度

题目来源 力扣2696删除子串后的字符串最小长度 题目概述 给你一个仅由 大写 英文字符组成的字符串 s 。 你可以对此字符串执行一些操作,在每一步操作中,你可以从 s 中删除 任一个 "AB" 或 "CD" 子字符串。 通过执行操作&#xff…

golang 记录一次协程和协程池的使用,利用ants协程池来处理定时器导致服务全部阻塞

前言 在实习的项目中有一个地方遇到了需要协程池的地方,在mt推荐下使用了ants库。因此在此篇记录一下自己学习使用此库的情况。 场景描述 此服务大致是一个kafka消息接收、发送相关。接收消息,根据参数设置定时器进行重发。 通过这里新建kafka服务&a…

淘宝商品数据采集订单数据采集店铺数据采集API演示

淘宝商品数据采集代码 -- 请求示例 url 默认请求参数已经URL编码处理 curl -i "https://api-gw.cn/taobao/item_get/?key<您自己的apiKey>&secret<您自己的apiSecret>&num_iid652874751412&is_promotion1" 商品详情页与获取到的返回数据示…

深入理解 Hadoop (五)YARN核心工作机制浅析

概述 YARN 的核心设计理念是 服务化&#xff08;Service&#xff09; 和 事件驱动&#xff08;Event EventHandler&#xff09;。服务化 和 事件驱动 软件设计思想的引入&#xff0c;使得 YARN 具有低耦合、高内聚的特点&#xff0c;各个模块只需完成各自功能&#xff0c;而模…

springcloud工地智慧云平台源码

智慧工地云平台是一种基于云计算和物联网技术的解决方案&#xff0c;旨在提升工地管理的效率和安全性。它通过集成多种传感器、设备和软件系统&#xff0c;实现对工地各个环节的实时监控、数据采集和分析&#xff0c;从而实现对工地运营的全面管理和智能化决策。 智慧工地利用…

java项目启动报错排查过程——mysql服务挂了

来自前端同事无法启动后台项目的案例 Caused by: java.net.ConnectException Create breakpoint : Connection refused: connect The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. 大致…

flex布局(3)

九、骰子 *{margin:0;padding: 0;box-sizing: border-box; } .flex{display: flex;flex-flow: row wrap;justify-content: space-between;align-items: center;align-content: space-between;padding:20px; } .touzi{width: 120px;height: 120px;background-color: aliceblue;…

SSL证书不受信任怎么办? SSL证书不受信任解决方案汇总

随着网络安全问题日益凸显&#xff0c;网站使用SSL证书以实现HTTPS加密及身份的可信认证&#xff0c;防止传输数据的泄露或篡改&#xff0c;已成为互联网人的共识。但SSL证书并不是部署了就能正常使用的&#xff0c;有时浏览器会提示“SSL证书不受信任”&#xff0c;这种时候该…

sg - 8503 ca双可选可编程晶体振荡器 (SPXO)

SG-8503CA可编程晶体振荡器&#xff0c;7050六引脚石英晶振&#xff0c;LV-PECL差分石英晶体振荡器&#xff0c;尺寸7.0 5.0 1.5mm轻薄型、低抖动、低耗能、低相位等特点&#xff0c;差分输出晶振非常适用于蓝牙模块、网络设备、智能家居、无线设备、OTN, BTS&#xff0c;测试…

vue配置qiankun及打包上线

项目结构 基座&#xff1a;vue3 子应用A&#xff1a;vue3 子应用B&#xff1a; react 子应用C&#xff1a;vue3vite 项目目录&#xff1a; 配置基座 首先下载qiankun yarn add qiankun # 或者 npm i qiankun -S 所有子应用也要安装&#xff0c;vue-vite项目安装 cnpm ins…