详解Spring系列之@ComponentScan自动扫描组件

目录
  • 无注解方式component-scan使用
  • 注解方式@ComponentScan使用
  • @ComponentScan的扫描规则

无注解方式component-scan使用

之前,我们需要扫描工程下一些类上所标注的注解,这些常用注解有:

@Controller,@Service,@Component,@Repository

通过在Spring的配置文件中配置<context:component-scan>扫描对应包下扫描这些注解的方式:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	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-2.5.xsd">
    <!--@Controller,@Service,@Component,@Repository-->
	<context:component-scan base-package="com.jektong.spring"/>
</beans>

注解方式@ComponentScan使用

建三个类,依次将

@Controller,@Repository,@Service,标注这些类:

图1

现在通过使用注解@ComponentScan的方式来扫描所在包下面的这些类:之前定义的PersonConfig修改:

package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.jektong.spring.Person;
@Configuration
@ComponentScan("com.jektong")
public class PersonConfig {
	@Bean("person01")
	public Person person() {
		return new Person("李四",21);
	}
}

测试,看是否扫描到这些注解所标注的类:PersonTest.java

@Test
public  void test02() {
	ApplicationContext ac = new AnnotationConfigApplicationContext(PersonConfig.class);
	Person bean = ac.getBean(Person.class);
	System.out.println(bean);
	String[] beanDefinitionNames = ac.getBeanDefinitionNames();
	for (String string : beanDefinitionNames) {
		System.out.println(string);
	}
}

测试效果:除了Spring要自动加载的配置类以外也显示了刚才添加的配置类:

图2

为何会出现PersonConfig,因为@Configuration本 身就是@Component注解的:

图3

@ComponentScan的扫描规则

如果需要指定配置类的扫描规则,@ComponentScan提供对应的扫描方式@Filter进行配置类的过滤:

// 扫描包的时候只规定扫描一些注解配置类。
Filter[] includeFilters() default {};
// 扫描包的时候可以排除一些注解配置类。
Filter[] excludeFilters() default {};

Filter其实也是一个注解,相当于@ComponentScan的子注解,可以看图4:

图4

Filter对应的过滤规则如下:

第一种:扫描包的时候只规定扫描一些注解配置类【includeFilters】。

使用这个includeFilters过滤规则,必须解除默认的过滤规则,

使用【useDefaultFilters = false】:

package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import com.jektong.spring.Person;
@Configuration
@ComponentScan(value = "com.jektong",includeFilters  = {
		@Filter(type = FilterType.ANNOTATION,value= {Controller.class})
},useDefaultFilters = false )
public class PersonConfig {
	@Bean("person01")
	public Person person() {
		return new Person("李四",21);
	}
}

这样就只会扫描用@Controller,标注的配置类交给Spring容器中了:

图5

第二种:扫描包的时候可以排除一些注解配置类【excludeFilters】。

图6

@Filter看上图,有5种不同类型的过滤策略。拿第一种举例,我们需要过滤使用@Controller注解的配置类:

package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import com.jektong.spring.Person;
@Configuration
@ComponentScan(value = "com.jektong",excludeFilters = {
		@Filter(type = FilterType.ANNOTATION,value= {Controller.class})
} )
public class PersonConfig {
	@Bean("person01")
	public Person person() {
		return new Person("李四",21);
	}
}

测试看一下发现图2中的personController不会交给Spring容器去管理了:

图7

上面的图6展示出5种不同类型的过滤策略,上面介绍了注解类型(FilterType.ANNOTATION),还有四种:

重点看一下CUSTOM自定义扫描策略。

从源码看,自定义扫描注解类型需要实现TypeFilter接口,下面就写一个实现类MyFilter.java:在实现类中可以自定义配置规则:

package com.jektong.config;
import java.io.IOException;
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;
public class MyFilter implements TypeFilter {
	@Override
	public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
			throws IOException {
		// 查看当前类的注解。
		AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
		// 查看当前扫描类的信息
		ClassMetadata classMetadata = metadataReader.getClassMetadata();
		// 获取当前类资源
		Resource resource = metadataReader.getResource();
		String className = classMetadata.getClassName();
		System.out.println("className===>" + className);
		// 只要类名包含er则注册Spring容器
		if(className.contains("er")) {
			return true;
		}
		return false;
	}
}

测试:

PersonConfig 中进行扫描:

package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import com.jektong.service.PersonService;
import com.jektong.spring.Person;
@Configuration
@ComponentScan(value = "com.jektong",includeFilters  = {
		@Filter(type = FilterType.CUSTOM,value= {MyFilter.class})
},useDefaultFilters = false )
public class PersonConfig {
	@Bean("person01")
	public Person person() {
		return new Person("李四",21);
	}
}

可以看出扫描出包下面的类只要带“er”的全部扫描出来,并配置给Spring容器:

ASSIGNABLE_TYPE:按照指定的类型去加载对应配置类:

package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import com.jektong.service.PersonService;
import com.jektong.spring.Person;
@Configuration
@ComponentScan(value = "com.jektong",includeFilters  = {
		@Filter(type = FilterType.ASSIGNABLE_TYPE,value= {PersonService.class})
},useDefaultFilters = false )
public class PersonConfig {
	@Bean("person01")
	public Person person() {
		return new Person("李四",21);
	}
}

尽管我们将PersonService.java上的注解去掉,使用ASSIGNABLE_TYPE依然会加载出来(自行测试)。

ASPECTJ与REGEX基本不用,不用了解。

以上就是@ComponentScan的具体用法,该兴趣的话可以看一下源码。

到此这篇关于详解Spring系列之@ComponentScan自动扫描组件的文章就介绍到这了,更多相关Spring @ComponentScan内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

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

    目录 SpringBoot默认包扫描机制 @ComponentScan的使用 常用参数含义 @Component与@ComponentScan SpringBoot默认包扫描机制 标注了@Component和@Component的衍生注解如@Controller,@Service,@Repository就可以把当前的Bean加入到IOC容器中.那么SpringBoot是如何知道要去扫描@Component注解的呢.@ComponentScan做的事情就是告诉Spring从哪里找到bean Spr

  • 详解Spring系列之@ComponentScan批量注册bean

    目录 回顾 本文内容 @ComponentScan基本原理和使用 基本原理 使用案例 定义配置类 容器扫描和使用 @ComponentScan进阶使用 源码简析 案例1:使用Filters过滤 案例2:使用自定义的bean名称生成策略 案例3:自定义bean的作用域策略 @Componet及其衍生注解使用 使用元注解和组合注解 总结 回顾 在前面的章节,我们介绍了@Comfiguration和@Bean结合AnnotationConfigApplicationContext零xml配置文件使用S

  • @ComponentScan在spring中无效的原因分析及解决方案

    目录 @ComponentScan在spring中无效 查了大量资料之后,找到了原因 @Component和@ComponentScan常规理解 @Component和@ComponentScan的联系 @SpringBootApplication和@ComponentScan,扫描包的区别 @ComponentScan在spring中无效 在我实现第一个spring AOP程序的时候,我按照主流的推荐,采用注解@ComponentScan @Aspect @Before 来实现一个切面. 让我

  • Spring注解开发@Bean和@ComponentScan使用案例

    组件注册 用@Bean来注册 搭建好maven web工程 pom加入spring-context,spring-core等核心依赖 创建实例类com.hjj.bean.Person, 生成getter,setter方法 public class Person { private String name; private int age; } 创建com.hjj.config.MainConfig @Configuration //告诉spring是一个配置类 public class Main

  • 为什么说要慎用SpringBoot @ComponentScan

    目录 场景复现 解密 解决方案 场景复现 为了统一定制一个过滤器(Filter),所以在另外一个工程里面创建了一个过滤器,并通过jar包的方法导入当前项目,通过@ComponentScan({"org.example.config"})指定扫描包路径. 下面的我的启动类: 导入的jar: 问题 预期效果是这样,正常加载 启动后,原来的Swagger目录进去是这样的,原来的Controller全部都没有被加载进来 解密 以为过滤器导致所有路径没有加载,后面百度了解BasicErrorCo

  • springboot @ComponentScan注解原理解析

    这篇文章主要介绍了springboot @ComponentScan注解原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 @ComponentScan 告诉Spring从哪里找到bean. 如果你的其他包都在@SpringBootApplication注解的启动类所在的包及其下级包,则你什么都不用做,SpringBoot会自动帮你把其他包都扫描了. 如果你有一些bean所在的包,不在启动类的包及其下级包,那么你需要手动加上@Compone

  • Springboot项目实现将类从@ComponentScan中排除

    目录 将类从@ComponentScan中排除 问题描述 方案一 方案二 方案三 方案四 @ComponentScan 详解 将类从@ComponentScan中排除 问题描述 最近在学习SpringCloud的Ribbon,在使用 @RibbonClient(name = "SPRINGCLOUD-P-DEPT", configuration = RibbonConfig.class) 为服务指定负载均衡策略的时候,根据Ribbon官方文档介绍,自定义的Ribbon配置类不允许被Sp

  • SpringBoot中@ComponentScan的使用详解

    目录 SpringBoot @ComponentScan的使用 SpringBoot @ComponentScan 作用 SpringBoot @ComponentScan的使用 SpringBoot的启动类中有一个@ComponentScan,之前项目由于这个注解造成打包失败,这里对于这个注解进行总结,防止下次遇到这个问题再被难住. 其实这个注解主要是针对于第三方jar包中注解的应用. 如果第三方包中没有使用注解那么就完全不需要使用这个注解 使用方式如图所示,这里扫描的是 maven项目的依赖

  • 详解Spring系列之@ComponentScan自动扫描组件

    目录 无注解方式component-scan使用 注解方式@ComponentScan使用 @ComponentScan的扫描规则 无注解方式component-scan使用 之前,我们需要扫描工程下一些类上所标注的注解,这些常用注解有: @Controller,@Service,@Component,@Repository 通过在Spring的配置文件中配置<context:component-scan>扫描对应包下扫描这些注解的方式: <beans xmlns="http:

  • 详解Spring Cloud负载均衡重要组件Ribbon中重要类的用法

    Ribbon是Spring Cloud Netflix全家桶中负责负载均衡的组件,它是一组类库的集合.通过Ribbon,程序员能在不涉及到具体实现细节的基础上"透明"地用到负载均衡,而不必在项目里过多地编写实现负载均衡的代码. 比如,在某个包含Eureka和Ribbon的集群中,某个服务(可以理解成一个jar包)被部署在多台服务器上,当多个服务使用者同时调用该服务时,这些并发的请求能被用一种合理的策略转发到各台服务器上. 事实上,在使用Spring Cloud的其它各种组件时,我们都能

  • 详解Spring框架入门

    一.什么是Spring Spring框架是由于软件开发的复杂性而创建的.Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅仅限于服务器端的开发.从简单性.可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益.Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架. ◆目的:解决企业应用开发的复杂性 ◆功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能 ◆范围:任何Java应用 二.

  • 详解Spring 中 Bean 的生命周期

    前言 这其实是一道面试题,是我在面试百度的时候被问到的,当时没有答出来(因为自己真的很菜),后来在网上寻找答案,看到也是一头雾水,直到看到了<Spring in action>这本书,书上有对Bean声明周期的大致解释,但是没有代码分析,所以就自己上网寻找资料,一定要把这个Bean生命周期弄明白! ​ 网上大部分都是验证的Bean 在面试问的生命周期,其实查阅JDK还有一个完整的Bean生命周期,这同时也验证了书是具有片面性的,最fresh 的资料还是查阅原始JDK!!! 一.Bean 的完整

  • 详解spring如何使用注解开发

    在Spring4之后,要使用注解开发,必须要保证aop的包导入了. 使用注解需要导入context约束,增加注解的支持. <?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance

  • 详解Spring bean的注解注入之@Autowired的原理及使用

    一.@Autowired 概念: @Autowired 注释,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. 通过 @Autowired的使用来消除 set ,get方法. 在使用@Autowired之前,我们对一个bean配置起属性时,用的是 <property name="属性名" value=" 属性值"/> 使用@Autowired之后,我们只需要在需要使用的地方使用一个@Autowired 就可以了. 代码使用: public

  • 详解Spring配置及事务的使用

    目录 1.事务概念 什么是事务? 事务的四个特性(ACID): 2.事务操作(模拟事务操作环境) 3.事务管理(Spring事务管理) 4.事务操作(注解声明式事务管理) 在 spring 配置文件,配置事务管理器 在 spring 配置文件,开启事务注解 在 service 类上面(或者 service 类里面方法上面)添加事务注解 5.事务操作(声明式事务管理参数配置) propagation(事务传播行为) ioslation(事务隔离级别) timeout(超时时间) readOnly(

  • 详解Spring的核心机制依赖注入

    详解Spring的核心机制依赖注入 对于一般的Java项目,他们都或多或少有一种依赖型的关系,也就是由一些互相协作的对象构成的.Spring把这种互相协作的关系称为依赖关系.如A组件调用B组件的方法,可称A组件依赖于B组件,依赖注入让Spring的Bean以配置文件组织在一起,而不是以硬编码的方式耦合在一起 一.理解依赖注入 依赖注入(Dependency Injection) = 控制反转(Inversion ofControl,IoC):当某个Java实例(调用者)需另一个Java实例(被调

  • 详解spring boot starter redis配置文件

    spring-boot-starter-Redis主要是通过配置RedisConnectionFactory中的相关参数去实现连接redis service. RedisConnectionFactory是一个接口,有如下4个具体的实现类,我们通常使用的是JedisConnectionFactory. 在spring boot的配置文件中redis的基本配置如下: # Redis服务器地址 spring.redis.host=192.168.0.58 # Redis服务器连接端口 spring.

随机推荐