sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

@ComponentScan用于类或接口上主要是指定扫描路径,spring会把指定路径下带有指定注解的类注册到IOC容器中。
https://blog.csdn.net/qq_38262266/article/details/107787748

一、@ComponentScan注解作用:(扫描指定注解的类注册到IOC容器中)

  • @ComponentScan用于类或接口上主要是指定扫描路径,spring会把指定路径下带有指定注解的类注册到IOC容器中。
  • 会被自动装配的注解包括@Controller、@Service、@Component、@Repository等等。
  • 其作用等同于<context:component-scan base-package="com.maple.learn" />配置。

 

二、@ComponentScan使用

常用属性如下:

  • basePackages、value:指定扫描路径,如果为空则以@ComponentScan注解的类所在的包为基本的扫描路径
  • basePackageClasses:指定具体扫描的类
  • includeFilters:指定满足Filter条件的类
  • excludeFilters:指定排除Filter条件的类
  • includeFilters和excludeFilters 的FilterType可选:ANNOTATION=注解类型默认、ASSIGNABLE_TYPE(指定固定类)、ASPECTJ(ASPECTJ类型)、REGEX(正则表达式)、CUSTOM(自定义类型),自定义的Filter需要实现TypeFilter接口

 

三、@ComponentScan的常见的配置如下:

  1. @ComponentScan(value="com.maple.learn",
  2.    excludeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)},
  3.    includeFilters = {@ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class})}
  4.         )
  5. public class SampleClass{
  6.    ……

 

posted on 2022-03-20 20:59  sunny123456  阅读(311)  评论(0编辑  收藏  举报