springboot bean扫描路径的实现

1:默认扫描启动类所在路径下所有的bean

2:可以在启动类中添加注解,手动指定扫描路径:

@ComponentScan(basePackages = {"com.xxx.service1.*","com.xxx.service2.**"})

补充:SpringBoot 是如何通过 @SpringBootApplication 扫描项目中的 Bean

原因

首先因为 XXXXXXXApplication 附带 @SpringBootApplication 注解,而 @SpringBootApplication 注解的层次如下:

SpringBootApplication
----@Inherited
----@SpringBootConfiguration
--------@Configuration
----@EnableAutoConfiguration
--------@Inherited
--------@AutoConfigurationPackage
------------@Inherited
------------@Import(AutoConfigurationPackages.Registrar.class)
--------@Import(AutoConfigurationImportSelector.class)
----@ComponentScan
--------@Repeatable(ComponentScans.class)

实现

可以看到 @SpringBootApplication 继承 @ComponentScan 与 @Configuration 用处如下;

扫描方法开始流程:

主要观察黄色方块的方法,是具体扫描路径的地方,具体实现流程如下:

获取 File 目录下的所有以 class 为结尾的文件后,扫描工作就完成了, 剩下的就是 spring 判断是否要管理此类的逻辑(例如:该类是否存在 @Component )

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。如有错误或未考虑完全的地方,望不吝赐教。

(0)

相关推荐

  • Springboot实现多线程注入bean的工具类操作

    场景: 使用springboot多线程,线程类无法自动注入需要的bean 解决方法: 通过工具类获取需要的bean 工具类代码: import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springfram

  • SpringBoot普通类获取spring容器中bean的操作

    前言 在spring框架中,是无法在普通类中通过注解注入实例的,因为sping框架在启动的时候,就会将标明交给spring容器管理的类进行实例化,并梳理他们彼此的依赖关系,进行注入,没有交给spring容器管理的普通类,是不会进行注入的,即使你使用了注入的相关注解.这个时候,如果我们需要在普通类中获取spring容器中的实例,就需要一些特定的方法,这里将整理一下如何在springboot中实现这样的方法. 创建springboot工程demo 项目结构图示 项目结构说明 service包下为de

  • springboot 实现bean手动注入操作

    1.springboot启动类实现接口ApplicationListener<ContextRefreshedEvent>,实现方法onApplicationEvent,初始化上下文 package test.projectTest; import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration; import org.springframework.boot.SpringApplication; import or

  • 解决springboot bean中大写的字段返回变成小写的问题

    例如我的bean中有以下4个字段 private String code; private String _TOKENUUMS; private String TGC; private String U; 在返回的json里只会显示 {"code":"xx","tgc":"xx","u":"xx"} 大小会变成小写,特殊符号开头的字段都不会显示,其原因是因为springboot在进行序列

  • 在springboot中实现个别bean懒加载的操作

    懒加载---就是我们在spring容器启动的是先不把所有的bean都加载到spring的容器中去,而是在当需要用的时候,才把这个对象实例化到容器中. @Lazy 在需要懒加载的bean上加上@Lazy就可以了 补充知识:springboot组件懒加载的坑及加载规则 什么是懒加载? 懒加载的意思是不在项目启动的时候实例出来这个组件 @RestController public class ApiController { @Autowired Skill kobSkillImpl; @Request

  • springboot2.x解决运行顺序及Bean对象注入顺序的问题

    1 前言 通过指定接口,重写指定方法,可以在Bean对应的生命周期方法中执行相应的程序 2 测试 本文将分析几个Bean对象,为它们设置优先级(通过@Order),然后再打断点调试,测试各种生命周期方法的运行的顺序 在项目当中最让人头疼的就是bean对象不被注入的问题,通过本文,你可以很好的解决这个问题. 先看看本程序使用的依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="

  • springboot bean扫描路径的实现

    1:默认扫描启动类所在路径下所有的bean 2:可以在启动类中添加注解,手动指定扫描路径: @ComponentScan(basePackages = {"com.xxx.service1.*","com.xxx.service2.**"}) 补充:SpringBoot 是如何通过 @SpringBootApplication 扫描项目中的 Bean 原因 首先因为 XXXXXXXApplication 附带 @SpringBootApplication 注解,而

  • springboot 无法扫描到父类模块中Bean的原因及解决

    目录 springboot 无法扫描到父类模块中Bean 现象: 如何解决 解决方案 spring boot 启动就自动关闭 之 找不到bean 解决方法: 原因: 以下收集别的解释: 所以有两种解决办法: springboot 无法扫描到父类模块中Bean 现象: 我定义了两个模块 A 和 B .B模块依赖A模块 A模块中我定义了一个@Component 却发现在B模块中我无法扫描到这个Bean导入注入失败 如何解决 查阅得知,在springboot中的bean扫描是扫描同级目录或者下级目录,

  • SpringBoot默认包扫描机制及@ComponentScan指定扫描路径详解

    目录 SpringBoot默认包扫描机制 @ComponentScan的使用 常用参数含义 @Component与@ComponentScan SpringBoot默认包扫描机制 标注了@Component和@Component的衍生注解如@Controller,@Service,@Repository就可以把当前的Bean加入到IOC容器中.那么SpringBoot是如何知道要去扫描@Component注解的呢.@ComponentScan做的事情就是告诉Spring从哪里找到bean Spr

  • springboot实现指定mybatis中mapper文件扫描路径

    目录 指定mybatis中mapper文件扫描路径 mybatis配置多个扫描路径写法 指定mybatis中mapper文件扫描路径 所有的mapper映射文件 mybatis.mapper-locations=classpath*:com/springboot/mapper/*.xml 或者resource下的mapper映射文件 mybatis.mapper-locations=classpath*:mapper/**/*.xml mybatis配置多个扫描路径写法 百度得到,但是很乱,稍微

  • Springboot自动扫描包路径来龙去脉示例详解

    我们暂且标注下Springboot启动过程中较为重要的逻辑方法,源码对应的spring-boot-2.2.2.RELEASE版本 public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection<SpringBoo

  • Spring中Bean扫描原理详情

    目录 前言 环境建设 正式开始 configureScanner 第一段代码 第二段代码 第三段代码 第四段代码 parseTypeFilters doScan findCandidateComponents For遍历每一个资源 isCandidateComponent(MetadataReader metadataReader) 继续后面的逻辑 isCandidateComponent(AnnotatedBeanDefintion) 继续后面的逻辑 doScan 继续后面的逻辑 doScan

  • springboot bean循环依赖实现以及源码分析

    前言 本文基于springboot版本2.5.1 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.1</version> <relativePath/> <!-- lookup parent from repositor

  • SpringBoot自定义/error路径失效的解决

    目录 SpringBoot自定义/error路径失效 背景 配置信息 解决思路 SpringBoot的/error的自定义处理 它的具体定义如下 他有三个方法 SpringBoot自定义/error路径失效 背景 最近使用SpringBoot做controller统一异常处理的时候,配置好映射路径(/error),使用SpringBoot自带的异常通知注解@ControllerAdvice配置好异常处理类,按理说在Controller发生异常的时候重定向到自定义错误页面(这里是重定向到Sprin

  • SpringBoot Bean花式注解方法示例下篇

    目录 1.容器初始化完成后注入bean 2.导入源的编程式处理 3.bean裁定 拓展 4.最终裁定 1.容器初始化完成后注入bean import lombok.Data; import org.springframework.stereotype.Component; @Component("miao") @Data public class Cat { } 被注入的JavaBean import org.springframework.context.annotation.Con

  • SpringBoot bean查询加载顺序流程详解

    目录 背景 探索-源码 进一步思考 背景 SpringBoot bean 加载顺序如何查看,想看加载了哪些bean, 这些bean的加载顺序是什么? 实际加载顺序不受控制,但会有一些大的原则: 1.按照字母顺序加载(同一文件夹下按照字母数序:不同文件夹下,先按照文件夹命名的字母顺序加载)2.不同的bean声明方式不同的加载时机,顺序总结:@ComponentScan > @Import > @Bean   这里的ComponentScan指@ComponentScan及其子注解,Bean指的是

随机推荐