Spring学习总结(6)-@Component和@ComponentScan注解

  参考文档:https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-stereotype-annotations

1、@Component

  @Component 被称为元注释,它是@Repository、@Service、@Controller、@Configuration的父类,理论上可以使用@Component来注释任何需要Spring自动装配的类。但每个注释都有他们自己的作用,用来区分类的作用,Spring文档上也说明后续版本中可能为@Repository、@Service、@Controller、@Configuration这些注释添加其他功能,所以建议大家还是少使用@Component。

  @Repository注解是任何满足存储库角色或构造型(也称为数据访问对象或DAO)的类的标记。该标记的用途之一是异常的自动翻译,如异常翻译中所述。

  @Service注解一般使用在Service层。

  @Controller注解一般使用在Controller层的类,@RestController继承了@Controller。

  @Configuration注解一般用来配置类,用来项目启动时加载的类。

 

2、@ComponentScan

  @ComponentScan注解一般用在Spring项目的核心配置类,或者在Spring Boot项目的启动类里使用。作用就是用来扫描@Component及其子类注释的类,用于Spring容器自动装配。@ComponentScan默认是扫描的路径是同级路径及同级路径的子目录,所以把Spring Boot的启动类放在根目录下,@ComponentScan是可以省略不写的。

  @ComponentScan的主要属性如下:

2.1 value属性、basePackages属性

 源码如下:

@AliasFor("basePackages")
String[] value() default {};

@AliasFor("value")
String[] basePackages() default {};

  这两个值的作用是一样的,是相互别名的关系。内容是填写要扫描的路径,如果是有一个路径,可以不用写value,有几种写法如下:

@ComponentScan("com.zh.service")

@ComponentScan(value = "com.zh.service")

@ComponentScan(value = {"com.zh.dao", "com.zh.service"})

 

2.2 includeFilters属性、excludeFilters属性

  这两个的作用都是过滤器,excludeFilters的作用是剔除values属性内的个别不需要加载的类,而includeFilters一般是和excludeFilters配合使用,就是被excludeFilters剔除的类里面,还需要其中的几个类,就用includeFilters再加上。

  举个例子,假设送了excludeFilters剔除了所有注解是Repository的类,但其中一个Stub开头的类,还要用到,就可以按下面的例子这样写。

源码如下:

    ComponentScan.Filter[] includeFilters() default {};

    ComponentScan.Filter[] excludeFilters() default {};

Filter作为过滤器的基本对象,的源码如下:

复制代码
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Filter {
    FilterType type() default FilterType.ANNOTATION;

    @AliasFor("classes")
    Class<?>[] value() default {};

@AliasFor(
"value") Class<?>[] classes() default {};
String[] pattern()
default {}; }
复制代码

FilterType是过滤器的类型,定义方式有几种,源码如下:

复制代码
public enum FilterType {
    ANNOTATION,
    ASSIGNABLE_TYPE,
    ASPECTJ,
    REGEX,
    CUSTOM;

    private FilterType() {
    }
}
复制代码

这几种类型,把官网上的内容翻译写一下,大概的描述就这样的:

 

我们分别举个例子:

复制代码
// 正则表达式、默认
@Configuration
@ComponentScan(basePackages = "org.example",
        includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"),
        excludeFilters = @ComponentScan.Filter(Repository.class))
public class AppConfig {
    ...
}

// 可分配的,直接指定类路径
@ComponentScan(basePackages = "org.example",
  excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, pattern = {"org.example.SomeClass"}))

复制代码
posted @   闲人鹤  阅读(2768)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示