@Component

@component (将普通JavaBean实例化到spring容器中,Spring容器统一管理,用起来不用自己new了,相当于配置文件中的<bean id="" class=""/>

@ComponentScan Spring和SpringBoot中的@Component 和@ComponentScan注解用法介绍和注意事项 - MRs.甘的博客 - CSDN博客

如果你理解了ComponentScan,你就理解了Spring

Spring是一个依赖注入(dependency injection)框架。所有的内容都是关于bean的定义及其依赖关系

定义Spring Beans的第一步是使用正确的注解@Component或@Service或@Repository

但是,Spring不知道你定义了某个bean除非它知道从哪里可以找到这个bean

ComponentScan做的事情就是告诉Spring从哪里找到bean

由你来定义哪些包需要被扫描。一旦你指定了,Spring将会将在被指定的包及其下级包(sub packages)中寻找bean

下面分别介绍在Spring Boot项目和非Spring Boot项目(如简单的JSP/Servlet或者Spring MVC应用)中如何定义Component Scan

Spring Boot项目

总结:

如果你的其他包都在使用了@SpringBootApplication注解的main
    app所在的包及其下级包,则你什么都不用做,SpringBoot会自动帮你把其他包都扫描了
   
    如果你有一些bean所在的包,不在main
    app的包及其下级包,那么你需要手动加上@ComponentScan注解并指定那个bean所在的包
    
举个例子,看下面定义的类

package com.demo.springboot;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class SpringbootApplication {public static void main(String[] args) {ApplicationContext applicationContext = SpringApplication.run(SpringbootApplication .class, args);for (String name : applicationContext.getBeanDefinitionNames()) {System.out.println(name);}}
}

类 SpringbootApplication 在com.demo.springboot包下,这个类使用了@SpringBootApplication注解,该注解定义了Spring将自动扫描包com.demo.springboot及其子包下的bean.
如果你项目中所有的类都定义在com.demo.springboot包及其子包下,那你不需要做任何事

但假如你一个类定义在包com.demo.somethingelse下,则你需要将这个新包也纳入扫描的范围,有两个方案可以达到这个目的。

方案1
定义@CoponentScan(“com.demo”)//可以看一下源码,@SpringBootApplication注解包含了@ComponentScan,scanBasePackages最为ComponentScan中basePackages别名,所以可以把包扫描放到@SpringBootApplication注解中

这么做扫描的范围扩大到整个父包com.demo

@ComponentScan(“com.demo”)
@SpringBootApplication
public class SpringbootApplication {

方案2
定义分别扫描两个包
@ComponentScan({“com.demo.springboot”,”com.demo.somethingelse”})

@ComponentScan({"com.demo.springboot","com.demo.somethingelse"})
@SpringBootApplication
public class SpringbootApplication {```
特别注意一下:如果使用了方案2,如果仅仅只写@ComponentScan({"com.demo.somethingelse"})将导致com.demo.springboot包下的类无法被扫描到(框架原始的默认扫描效果无效了)### 在某个类上使用@Component注解,表明当需要创建类时,这个被注解的类是一个候选类。就像是举手。
### @ComponentScan用于扫描指定包下的类。就像看都有哪些举手了。

@Component和@ComponentScan相关推荐

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

    SpringBoot默认包扫描机制及@ComponentScan指定扫描路径详解 SpringBoot默认包扫描机制 标注了@Component和@Component的衍生注解如@Controller ...

  2. Spring高级之注解@ComponentScan详解(超详细)

    定义/作用 @ComponentScan注解用于实现spring主键的注解扫描,会扫描特定包内的类上的注解. 源码(对属性进行一些简介,会在后文中详细讲解每个属性): @Retention(Reten ...

  3. springboot 扫描jar包中bean_详解Spring Boot的Component Scan原理

    本文将帮助您了解Spring中最重要的概念 - 组件扫描.Spring Boot在组件扫描方面做了一些魔术 @ComponentScan 如果你了解组件扫描,你就会理解Spring.Spring是一个 ...

  4. @Component什么时候使用?

    通过本文你将学到: Component Scan是什么? 为什么ComponentScan很重要? 项目中Spring Boot会对哪些包自动执行扫描(Component Scan)? 如何利用Spr ...

  5. Springboot注解@ServletComponentScan和@ComponentScan

    文章目录 一.SpringBoot中使用Servlet 二.Spring, Spring Boot中的@ComponentScan注解用法介绍 三.使用@ComponentScan自动扫描组件实例 一 ...

  6. Spring注解详解:@ComponentScan自动扫描组件使用

    目录 无注解方式component-scan使用 注解方式@ComponentScan使用 @ComponentScan的扫描规则 无注解方式component-scan使用 之前,我们需要扫描工程下 ...

  7. java parentcomponent_java – 在Spring中继承@Component

    我有一个类的层次结构.我想用@Component标记它们.我试图只标记父类.我希望Spring也将孩子视为组件.但它不会发生. 我尝试使用自定义的@InheritedComponent注释,如here ...

  8. Spring源码解析之@Component注解的扫描

    阅读须知 Spring源码版本:4.3.8 文章中使用/* */注释的方法会做深入分析 正文 承接Spring源码解析之context:component-scan标签解析,下面就是扫描的流程: Cl ...

  9. SpringBoot默认包扫描机制及使用@ComponentScan指定扫描路径

    SpringBoot默认包扫描机制 标注了@Component和@Component的衍生注解如@Controller,@Service,@Repository就可以把当前的Bean加入到IOC容器中 ...

最新文章

  1. linux脚本 程序输入,[转]Linux中shell脚本如何自动输入…
  2. python菜鸟教程h-Python 命令行参数
  3. 用java调用oracle存储过程总结二
  4. 第四次上课 PPT作业
  5. Angular Component代码和编译后生成的JavaScript代码
  6. oracle数据库多表联查并排序,oracle数据库之多表联接查询(一)
  7. mysql读写分离和分布式_MySQL主从复制与读写分离
  8. fabric.js和高级画板
  9. “秃如其来”的90后,造就了一个蒸蒸日上的产业
  10. 女生学计算机好还是工业工程好,工业工程专业排名-工业工程考证我是学IE大三的,我想问一下,工业工程,有哪些与专业 爱问知识人...
  11. mysql的time格式化_【mysql格式化日期】
  12. MAC上编译OpenCV
  13. mybatis-plus 自定义UpdateWrapper(二)实现列的case set
  14. 查询系统css portal,详细论述 Portal 的样式单 --- Portal.css
  15. 玩转EXCEL系列-选择性粘贴几个实用技巧
  16. python的pygame模拟太阳-地球-月亮-金星等动态示意图代码分析
  17. linux iio设备
  18. realme有升级到android10的,RealmeX/Q将升级安卓10底层刷机包
  19. unity进度条,物品随机掉落
  20. C++函数之递归调用

热门文章

  1. (数据库系统概论|王珊)第一章绪论:习题
  2. 全国计算机三级考试网络技术--应用题总结
  3. 微软的黑屏说明了什么?
  4. 基于SSM旅游纪念品购物网站(idea-javaweb-javaee-j2ee-springboot)订单管理-购物评价-会员管理-购物车实现
  5. linux环境下php安装sqlsrv扩展连接mssql
  6. Ant Design Pro右上角个人设置管理
  7. 补码的加减法运算及溢出判断
  8. 应该去看看的十大论坛!!!
  9. 电磁流体力学类毕业论文文献包含哪些?
  10. 数字孪生开发平台 数字孪生开发成本 数字孪生开发平台cortona3d