Spring注解之@ComponentScan

【1】@ComponentScan注解是什么

@ComponentScan主要就是定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中

【2】@ComponentScan注解的详细使用

1、创建一个配置类,在配置类上添加 @ComponentScan 注解。该注解默认会扫描该类所在的包下所有的类

package com.zl.springbase.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan
public class MyConfig {
}

我的类结构如下:类MyDao上面添加了@Repository注解

测试程序:

public class TestComponentScan {
@Test
public void test1(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
String[] names = context.getBeanDefinitionNames();
for (String name : names) {
System.out.println("beanName"+name);
}
}
}
/**
beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: myConfig
beanName: myDao
*/

2、指定要扫描的包(使用@ComponentScan 的 valule 属性来配置),创建一个controller 包,并在该包下新建一个 MyController 类并添加@Controller注解。

@Configuration
@ComponentScan(value = {"com.zl.springBase.controller"})
public class MyConfig {
} /**测试结果
beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: myConfig
beanName: myController
*/

3、excludeFilters 和 includeFilters 的使用

  • excludeFilters

①使用 excludeFilters 来按照注解类型排除某些包的扫描

@Configuration
@ComponentScan(value = {"com.zl.springBase"},excludeFilters = {
@Filter(type = FilterType.ANNOTATION,value = {Controller.class})
})
public class MyConfig {
} /**测试结果
beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: myConfig
beanName: myDao
*/

​ excludeFilters 的参数是一个 Filter[] 数组,然后指定 FilterType 的类型为 ANNOTATION,也就是通过注解来过滤,最后的 value 则是Controller 注解类。配置之后,在 spring 扫描的时候,就会跳过 com.zl.springBase包下,所有被 @Controller 注解标注的类。

②使用 excludeFilters 来按照指定类型排除某些包的扫描

@Configuration
@ComponentScan(value = {"com.zl.springBase"},excludeFilters = {
@Filter(type = FilterType.ASSIGNABLE_TYPE,value = {MyDao.class})
})
public class MyConfig {
} /**测试结果
beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: myConfig
beanName: myController
*/

FilterType 的类型为 ASSIGNABLE_TYPE,就是通过指定类型来过滤,value指定为MyDao,则会过滤掉MyDao这个类。

③使用 excludeFilters 来按照自定义类型排除某些包的扫描

首先创建一个实现 TypeFilter 接口的 CustomTypeFilter 类,并实现其 match 方法。

这里简单对扫描到的类名进行判断,如果类名包含”Co“的就符合条件,也就会注入到容器中。

public class CustomTypeFilter implements TypeFilter {
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
//获取到当前扫描到的类的注解信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
//获取到当前扫描到的类的信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
//获取到当前扫描到的类的资源
Resource resource = metadataReader.getResource();
if (classMetadata.getClassName().contains("Co")) {
return true;
}
return false;
}
}

MyConfig:

@Configuration
@ComponentScan(value = {"com.zl.springBase"},excludeFilters = {
@Filter(type = FilterType.CUSTOM,value = {CustomTypeFilter.class})
})
public class MyConfig {
} /**测试结果
beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: myConfig
beanName: myDao
*/

可以看到MyController已经被过滤掉了,myCofig之所以没有被过滤掉是因为它本身是一个配置类,@Configuration注解类里面包含了@Component注解,所以IOC容器启动后MyConfig会被自动加入容器当中。

  • includeFilters

使用 includeFilters 来按照规则只包含某些包的扫描

在创建一个 service 的包,并创建一个 MyService 类,再加上一个 @Service 注解

修改MyConfig类:

@Configuration
@ComponentScan(value = {"com.zl.springBase"},includeFilters = {
@Filter(type = FilterType.ANNOTATION,value = {Controller.class})
})
public class MyConfig {
} /**测试结果
beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: myConfig
beanName: myDao
beanName: myController
beanName: myService
*/

​ 配置里面,应该是只包含 @Controller 注解的类才会被注册到容器中,为什么 @Service,@Repository 注解的类也被注册了呢?这里涉及到 @ComponentScan 的一个 useDefaultFilters 属性的用法,该属性默认值为 true,也就是说 spring 默认会自动发现被 @Component、@Repository、@Service 和 @Controller 标注的类,并注册进容器中。要达到只包含某些包的扫描效果,就必须将这个默认行为给禁用掉(在 @ComponentScan 中将 useDefaultFilters 设为 false 即可)。

一、Spring注解之@ComponentScan的更多相关文章

  1. 深入理解spring注解之@ComponentScan注解

    今天主要从以下几个方面来介绍一下@ComponentScan注解: @ComponentScan注解是什么 @ComponentScan注解的详细使用 1,@ComponentScan注解是什么 其实 ...

  2. Spring 注解<context:annotation-config> 和 <context:component-scan>的作用与区别

    <context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过packagesanning的方式)上面的注解 ...

  3. spring注解开发:ComponentScan组件扫描

    在使用xml方式配置时,我们只需要在xml中配置如下代码: <context:component-scan base-package="包名"></context ...

  4. spring注解注入:<context:component-scan>以及其中的context:include-filter>和 <context:exclude-filter>的是干什么的?

    转自:https://www.cnblogs.com/vanl/p/5733655.html spring注解注入:<context:component-scan>使用说明   sprin ...

  5. 【Spring注解驱动开发】自定义TypeFilter指定@ComponentScan注解的过滤规则

    写在前面 Spring的强大之处不仅仅是提供了IOC容器,能够通过过滤规则指定排除和只包含哪些组件,它还能够通过自定义TypeFilter来指定过滤规则.如果Spring内置的过滤规则不能够满足我们的 ...

  6. spring注解说明之Spring2.5 注解介绍(3.0通用)

    spring注解说明之Spring2.5 注解介绍(3.0通用) 注册注解处理器 方式一:bean <bean class="org.springframework.beans.fac ...

  7. 使用Spring注解来简化ssh框架的代码编写

     目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写. 首先:我们浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="m ...

  8. spring注解scheduled实现定时任务

    只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...

  9. [转]Spring 注解总结

    原文地址:http://blog.csdn.net/wangshfa/article/details/9712379 一 注解优点?注解解决了什么问题,为什么要使用注解? 二 注解的来龙去脉(历史) ...

随机推荐

  1. 基于python的selenium常用操作方法(1)

    1 selenium定位方法    Selenium提供了8种定位方式. ·         id ·         name ·         class name ·         tag ...

  2. Jenkins操作学习 --初始化安装

    前言 说到持续集成,可以说是当下比较热门的话题了,也是很多公司和It从业者推崇的热门技术,但在项目中真正实际应用起来的并不太多,但通过持续集成带来的好处还是值得学习和推广的. 1.什么是jenkins ...

  3. 解决 layui 弹出层(弹框)一闪而过就消失的问题 (转载)

    转载: 原文链接:https://blog.csdn.net/qq_20594019/article/details/83956532 本人遇到问题:使用layer.open()弹出页面层,出现弹框闪 ...

  4. linux下使用mv将递归的文件从多个目录移动到一个目录中

    find /data/download/temp \( -iname '*.mp4' -o -iname '*.avi' \) -type f -exec mv -nv -t '/data/downl ...

  5. log4net快速上手

    原文地址:https://www.cnblogs.com/lsgsanxiao/p/5845300.html 略有删改 1.配置文件,可以单独创建log4net.config文件,然后手动指定目录,也 ...

  6. MVC教程:MVC区域路由

    一.区域路由 为了管理网站中大量的文件,在ASP.NET MVC 2.0版本中引入了一个新概念:区域(Area). 有了区域以后,可以让我们的项目不至于太复杂而导致管理混乱.每个模块的页面都放入相应的 ...

  7. python基础(1):python介绍、python发展史

    1. python介绍 1.1 python是什么样的语言 编程语⾔主要从以下⼏个⻆度为进⾏分类,编译型和解释型.静态语⾔和动态语⾔.强类型定义语⾔和弱类型定义语⾔,我们先看编译型语⾔和解释型语⾔.稍 ...

  8. FCC---Make a CSS Heartbeat using an Infinite Animation Count----超级好看的心跳,粉色的

    Here's one more continuous animation example with the animation-iteration-count property that uses t ...

  9. 【LeetCode】746. 使用最小花费爬楼梯

    使用最小花费爬楼梯 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或 ...

  10. 熟悉的味道——从Java单例写到C++单例

    设计模式中,单例模式是常见的一种.单例模式需要满足以下两个条件: 保证一个类只能创建一个示例: 提供对该实例的全局访问点. 关于单例最经典的问题就是DCL(Double-Checked Lock),今 ...