Spring注解(二):@ComponentScan自动扫描组件

简介: 前一篇文章:Spring注解(一):@Configuration、@Bean给容器中注册组件,提到了如何通过使用配置文件的方式和注解的方式进行简单的组件注册。这里将介绍如何使用@ComponentScan注解进行组件的自动扫描。

前一篇文章:Spring注解(一):@Configuration、@Bean给容器中注册组件,提到了如何通过使用配置文件的方式和注解的方式进行简单的组件注册。这里将介绍如何使用@ComponentScan注解进行组件的自动扫描。


在上一篇代码的基础之上,如果通过配置文件的方式进行组件扫描,则需要在配置文件中使用context:component-scan标签元素,beans.xml文件如下:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
   <!--  包扫描,只要注解了@controller @service @Repository @component四个注解中的任何一个注解的组件都会被扫描加载到容器中-->
   <context:component-scan base-package="com.xinyi"></context:component-scan>
     <bean id="Person" class="com.xinyi.bean.Person">
         <property name="name" value="新一"></property>
         <property name="age" value="18"></property>
     </bean> 
</beans>
复制代码


如果采用注解的方式进行组件扫描,则需要在配置类中添加一个@ComponentScan组件:


package com.xinyi.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.xinyi.bean.Person;
//配置类等同于以前的配置文件
//@Configuration等同于告诉spring这是一个配置类
@Configuration
//value指定要扫描的包,默认是该包下组件的都会被扫描
@ComponentScan(value="com.xinyi")
public class MyConfig {
  //@Bean给容器中注入一个bean,类型为返回值得类型,id默认方法名作为id
  //注入person得值
  @Bean("person222")
  public Person person111() {
    return new Person("xinyi、",19);
  }
}
复制代码


然后新建一个controller,service和dao:


package com.xinyi.controller;
import org.springframework.stereotype.Controller;
//被@Controller标记的类实际上就是个Controller对象(即控制器类),分发
//处理器会扫描使用该注解的类的方法,并检测该方法是否使用了@RequestMapping注解。
//@Controller只是定义了一个控制器类,而使用@RequestMapping注解的方法才是处理请求的处理器。
@Controller
public class MyController {
}
复制代码


package com.xinyi.service;
import org.springframework.stereotype.Service;
//@Service注解表示该类是业务层,
@Service
public class MyService {
}
复制代码


package com.xinyi.dao;
import org.springframework.stereotype.Repository;
//@Repository注解修饰哪个类,表示该类属于dao层,具有进行CRUD的功能
@Repository
public class MyDao {
}
复制代码


新建一个测试类,输出扫描的组件有哪些:


package com.xinyi.test;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.xinyi.config.MyConfig;
public class Test {
  @Test
  public void test1() {
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
    String[] strings = applicationContext.getBeanDefinitionNames();
    for(String string : strings) {
      System.out.println(string);
    }
  }
}
复制代码


则被输出的组件有:


myConfig
myController
myDao
myService
person222
复制代码


另外@ComponentScan注解的includeFilters和ExcludeFilters属性能够指定扫描的组件和排除指定不扫描的组件,

includeFilters和excludeFilters属性接受的参数都是数组的形式。


/**
   * Specifies which types are eligible for component scanning.
   * <p>Further narrows the set of candidate components from everything in {@link #basePackages}
   * to everything in the base packages that matches the given filter or filters.
   * <p>Note that these filters will be applied in addition to the default filters, if specified.
   * Any type under the specified base packages which matches a given filter will be included,
   * even if it does not match the default filters (i.e. is not annotated with {@code @Component}).
   * @see #resourcePattern()
   * @see #useDefaultFilters()
   */
  Filter[] includeFilters() default {};
  /**
   * Specifies which types are not eligible for component scanning.
   * @see #resourcePattern
   */
  Filter[] excludeFilters() default {};
复制代码


ExcludeFilters指定不扫描的组件:


package com.xinyi.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import com.xinyi.bean.Person;
//excludeFilters:指定不扫描的组件 这里是按照注解的方式排除Service和Repository
@ComponentScan(value="com.xinyi",excludeFilters = {@Filter(type=FilterType.ANNOTATION,classes = {Service.class,Repository.class})})
public class MyConfig {
  //@Bean给容器中注入一个bean,类型为返回值得类型,id默认方法名作为id
  //注入person得值
  @Bean("person222")
  public Person person111() {
    return new Person("xinyi、",19);
  }
}
复制代码


输出的结果:


//排除了dao和service
myConfig
myController
person222
复制代码


IncludeFilters指定扫描的组件,includeFilters属性有个参数为useDefaultFilters,默认为true,表示默认扫描所有的包,所以如果添加过滤规则扫描指定的组件,则需要将useDefaultFilters=false:


package com.xinyi.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import com.xinyi.bean.Person;
@ComponentScan(value="com.xinyi",includeFilters = {@Filter(type=FilterType.ANNOTATION,classes = {Service.class,Repository.class})},useDefaultFilters = false)
public class MyConfig {
  //@Bean给容器中注入一个bean,类型为返回值的类型,id默认方法名作为id
  //注入person得值
  @Bean("person222")
  public Person person111() {
    return new Person("xinyi、",19);
  }
}
复制代码


输出的组件为:


//只包好了service和dao,并没有扫描到controller
myConfig
myDao
myService
person222
复制代码


在定义包扫描规则中,通过使用@Filter注解的方式指定过滤规则,上述代码是按照注解的方式进行过滤,FilterType是一个枚举类,定义了各种过滤类型:


public enum FilterType {
  /**
   * Filter candidates marked with a given annotation.
   * @see org.springframework.core.type.filter.AnnotationTypeFilter
   */
   //按照注解的方式
  ANNOTATION,
  /**
   * Filter candidates assignable to a given type.
   * @see org.springframework.core.type.filter.AssignableTypeFilter
   */
   //按照给定的类型
  ASSIGNABLE_TYPE,
  /**
   * Filter candidates matching a given AspectJ type pattern expression.
   * @see org.springframework.core.type.filter.AspectJTypeFilter
   */
     //按照ASPECTJ表达式
  ASPECTJ,
  /**
   * Filter candidates matching a given regex pattern.
   * @see org.springframework.core.type.filter.RegexPatternTypeFilter
   */
    //按照正则表达式
  REGEX,
  /** Filter candidates using a given custom
   * {@link org.springframework.core.type.filter.TypeFilter} implementation.
   */
    //按照自定义规则
  CUSTOM
}
复制代码


以上各种过滤类型的使用方法和注解类型使用方式一样,使用较多的是按照指定类型和自定义规则进行过滤,比如:


1、根据指定的类型过滤:


@ComponentScan(value="com.xinyi",includeFilters = {@Filter(type=FilterType.ASSIGNABLE_TYPE,classes = {MyDao.class})},useDefaultFilters = false)
复制代码


2、根据自定义的规则过滤:


首先需要新建一个自定义过滤规则:


package com.xinyi.config;
import java.io.IOException;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
//实现TypeFilter接口
public class MyTypeFilter implements TypeFilter {
  //metadataReader:读取到当前正在扫描的类
  //metadataReaderFactory:读取到其他任何类的信息
  //match方法返回boolean类型的值,true表示匹配,符合过滤规则,false则不匹配
  public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
      throws IOException {
    // TODO Auto-generated method stub
    //获取当前类注解的信息
    AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
    //获取正在扫描的类的信息
    ClassMetadata classMetadata = metadataReader.getClassMetadata();
    //获取当前类资源信息(路径信息)
    Resource resource = metadataReader.getResource();
    String classnameString = classMetadata.getClassName();
    System.out.println("正在扫描的类:"+classnameString);
    if(classnameString.contains("er")) {
      return true;
    }
    return false;
  }
}
复制代码


使用自定义过滤规则:


@ComponentScan(value="com.xinyi",includeFilters = {@Filter(type=FilterType.CUSTOM,classes = {MyTypeFilter.class})},useDefaultFilters = false)
复制代码


输出结果:


8e6cd168727240189d2d1675093fe1f9~tplv-k3u1fbpfcp-zoom-in-crop-mark_1304_0_0_0.webp.jpg


@ComponentScans注解,等同于多个@ComponentScan注解(在一个类中可以同时写多个@ComponentScan注解,定义多种包扫描过滤规则),所以如果定义多种组件扫描的过滤规则,可以使用@ComponentScans注解:


@ComponentScans(value = {
@ComponentScan(value="com.xinyi",includeFilters = {@Filter(type=FilterType.ANNOTATION,classes = {Service.class,Repository.class})},useDefaultFilters = false),... })
复制代码


以上就是注解开发中如何使用@ComponentScan注解进行包扫描,以及使用IncludeFilters和ExcludeFilters属性,定义包扫描过滤规则。

目录
相关文章
|
缓存 监控 Java
SpringBoot @Scheduled 注解详解
使用`@Scheduled`注解实现方法周期性执行,支持固定间隔、延迟或Cron表达式触发,基于Spring Task,适用于日志清理、数据同步等定时任务场景。需启用`@EnableScheduling`,注意线程阻塞与分布式重复问题,推荐结合`@Async`异步处理,提升任务调度效率。
1969 128
|
Java 测试技术 数据库
使用Spring的@Retryable注解进行自动重试
在现代软件开发中,容错性和弹性至关重要。Spring框架提供的`@Retryable`注解为处理瞬时故障提供了一种声明式、可配置的重试机制,使开发者能够以简洁的方式增强应用的自我恢复能力。本文深入解析了`@Retryable`的使用方法及其参数配置,并结合`@Recover`实现失败回退策略,帮助构建更健壮、可靠的应用程序。
1390 1
使用Spring的@Retryable注解进行自动重试
|
XML Java 数据格式
常用SpringBoot注解汇总与用法说明
这些注解的使用和组合是Spring Boot快速开发和微服务实现的基础,通过它们,可以有效地指导Spring容器进行类发现、自动装配、配置、代理和管理等核心功能。开发者应当根据项目实际需求,运用这些注解来优化代码结构和服务逻辑。
861 12
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
1079 2
|
传感器 Java 数据库
探索Spring Boot的@Conditional注解的上下文配置
Spring Boot 的 `@Conditional` 注解可根据不同条件动态控制 Bean 的加载,提升应用的灵活性与可配置性。本文深入解析其用法与优势,并结合实例展示如何通过自定义条件类实现环境适配的智能配置。
887 0
探索Spring Boot的@Conditional注解的上下文配置
|
智能设计 Java 测试技术
Spring中最大化@Lazy注解,实现资源高效利用
本文深入探讨了 Spring 框架中的 `@Lazy` 注解,介绍了其在资源管理和性能优化中的作用。通过延迟初始化 Bean,`@Lazy` 可显著提升应用启动速度,合理利用系统资源,并增强对 Bean 生命周期的控制。文章还分析了 `@Lazy` 的工作机制、使用场景、最佳实践以及常见陷阱与解决方案,帮助开发者更高效地构建可扩展、高性能的 Spring 应用程序。
586 0
Spring中最大化@Lazy注解,实现资源高效利用
|
Java 测试技术 编译器
@GrpcService使用注解在 Spring Boot 中开始使用 gRPC
本文介绍了如何在Spring Boot应用中集成gRPC框架,使用`@GrpcService`注解实现高效、可扩展的服务间通信。内容涵盖gRPC与Protocol Buffers的原理、环境配置、服务定义与实现、测试方法等,帮助开发者快速构建高性能的微服务系统。
2139 0
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
1681 0
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
1710 0
|
前端开发 Java 数据库
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 介绍
本课介绍Spring Boot集成Thymeleaf模板引擎。Thymeleaf是一款现代服务器端Java模板引擎,支持Web和独立环境,可实现自然模板开发,便于团队协作。与传统JSP不同,Thymeleaf模板可以直接在浏览器中打开,方便前端人员查看静态原型。通过在HTML标签中添加扩展属性(如`th:text`),Thymeleaf能够在服务运行时动态替换内容,展示数据库中的数据,同时兼容静态页面展示,为开发带来灵活性和便利性。
714 0