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"}))

原文地址:https://www.cnblogs.com/huanshilang/p/11745698.html