iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Spring之@ComponentScan自动扫描组件怎么使用
  • 900
分享到

Spring之@ComponentScan自动扫描组件怎么使用

2023-07-02 09:07:56 900人浏览 薄情痞子
摘要

这篇文章主要讲解了“spring之@ComponentScan自动扫描组件怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring之@ComponentScan自动扫描组件怎么使用

这篇文章主要讲解了“spring之@ComponentScan自动扫描组件怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring之@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,标注这些类:

Spring之@ComponentScan自动扫描组件怎么使用

图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

@Testpublic  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要自动加载的配置类以外也显示了刚才添加的配置类:

Spring之@ComponentScan自动扫描组件怎么使用

图2

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

Spring之@ComponentScan自动扫描组件怎么使用

图3

@ComponentScan的扫描规则

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

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

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

Spring之@ComponentScan自动扫描组件怎么使用

图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容器中了:

Spring之@ComponentScan自动扫描组件怎么使用

图5

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

Spring之@ComponentScan自动扫描组件怎么使用

图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容器去管理了:

Spring之@ComponentScan自动扫描组件怎么使用

图7

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

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

Spring之@ComponentScan自动扫描组件怎么使用

源码看,自定义扫描注解类型需要实现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 {@Overridepublic 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容器:

Spring之@ComponentScan自动扫描组件怎么使用

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基本不用,不用了解。

感谢各位的阅读,以上就是“Spring之@ComponentScan自动扫描组件怎么使用”的内容了,经过本文的学习后,相信大家对Spring之@ComponentScan自动扫描组件怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

--结束END--

本文标题: Spring之@ComponentScan自动扫描组件怎么使用

本文链接: https://www.lsjlt.com/news/340893.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Spring之@ComponentScan自动扫描组件怎么使用
    这篇文章主要讲解了“Spring之@ComponentScan自动扫描组件怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring之@ComponentScan自动扫描组件怎么使用...
    99+
    2023-07-02
  • 详解Spring系列之@ComponentScan自动扫描组件
    目录无注解方式component-scan使用注解方式@ComponentScan使用@ComponentScan的扫描规则无注解方式component-scan使用 之前,我们需要...
    99+
    2024-04-02
  • spring中@ComponentScan自动扫描并指定扫描规则
    目录1.使用注解配置包扫描1.1.创建相关类1.2.SpringBoot启动类默认就有配置@ComponentScan1.3.查看IOC中的bean的名称2.扫描时排除注解标注的类3...
    99+
    2023-05-18
    @ComponentScan自动扫描 spring @ComponentScan扫描
  • Spring@ComponentScan注解扫描组件原理
    目录ConfigurationClassPostProcessor类Parse each @Configuration classComponentScanAnnotationPar...
    99+
    2023-01-10
    Spring @ComponentScan注解 Spring @ComponentScan Spring @ComponentScan扫描组件
  • Spring装配Bean之如何实现组件扫描和自动装配
    这篇文章给大家分享的是有关Spring装配Bean之如何实现组件扫描和自动装配的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Spring从两个角度来实现自动化装配:组件扫描:Spring会自动发现应用上下文中所创...
    99+
    2023-05-31
    spring bean
  • spring boot自动装配之@ComponentScan注解用法详解
    目录1.@ComponentScan注解作用2. @ComponentScan注解属性3. @ComponentScan过滤规则说明4. 自定义扫描过滤规则5. @Component...
    99+
    2023-05-18
    @componentscan注解 @componentscan注解用法 componentscan作用
  • win10扫描仪怎么使用
    要使用Windows 10中的扫描仪,您可以按照以下步骤操作:1. 连接您的扫描仪到计算机。确保扫描仪已经正确安装并连接好。2. 打...
    99+
    2023-09-09
    win10
  • 使用@Value值注入及配置文件组件扫描
    @Value值注入及配置文件组件扫描 spring配置文件对应的是父容器,springMVC配置文件产生的是子容器,前者一般配置数据源,事务,注解等,当然还可以进一步将一些配置细化到...
    99+
    2024-04-02
  • 如何使用@Value值注入及配置文件组件扫描
    本篇内容介绍了“如何使用@Value值注入及配置文件组件扫描”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!@Value值注入及配置文件组件扫...
    99+
    2023-06-20
  • QingScan扫描器怎么安装使用
    本篇内容主要讲解“QingScan扫描器怎么安装使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“QingScan扫描器怎么安装使用”吧!一、 QingScan介绍QingScan是一个安全工具...
    99+
    2023-06-22
  • C#怎么使用Twain协议实现扫描仪连续扫描功能
    本篇内容主要讲解“C#怎么使用Twain协议实现扫描仪连续扫描功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#怎么使用Twain协议实现扫描仪连续扫描功能”吧!C#调用Twain接口实现扫...
    99+
    2023-06-26
  • 使用python怎么扫描web邮箱
    这篇文章给大家介绍使用python怎么扫描web邮箱,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。基本思路我们向工具传入目标站点之后,首先要对输入进行一个基本的检查和分析,因为我们会可能会传入各种样式的地址,比如htt...
    99+
    2023-06-14
  • php漏洞扫描工具怎么使用
    使用php漏洞扫描工具的步骤如下:1. 下载和安装php漏洞扫描工具,例如:OWASP ZAP、Netsparker等。2. 打开漏...
    99+
    2023-06-08
    php漏洞扫描工具 php
  • 使用java怎么扫描指定包下的类
    本篇文章给大家分享的是有关使用java怎么扫描指定包下的类,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。Java可以用来干什么Java主要应用于:1. web开发;2. And...
    99+
    2023-06-14
  • Linux的nmap扫描端口命令怎么使用
    本篇内容介绍了“Linux的nmap扫描端口命令怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!nmap扫描端口命令是“nmap -s...
    99+
    2023-07-04
  • SonarQube实现自动化代码扫描的安装及使用集成方式
    目录1、安装Findbugs插件2、IDEA集成3、Gitlab集成4、Jenkins集成1、安装Findbugs插件 Sonar有自己的默认的扫描规则,可通过安装Findbugs插...
    99+
    2024-04-02
  • 爬虫进阶-JS自动渲染之Scrapy_splash组件的使用
    目录1. 什么是scrapy_splash?2. scrapy_splash的作用3. scrapy_splash的环境安装3.1 使用splash的docker镜像3.2 在pyt...
    99+
    2024-04-02
  • Vue3之Teleport组件怎么使用
    Teleport 组件解决的问题版本:3.2.31如果要实现一个 “蒙层” 的功能,并且该 “蒙层” 可以遮挡页面上的所有元素,通常情况下我们会选择直接在 标签下渲染 “蒙层” 内容。如果在Vue.js 2 中实现这个功能,只能通过原生 D...
    99+
    2023-05-14
    Vue3 teleport
  • 网络扫描管理软件Network Radar for Mac怎么用
    这篇文章给大家分享的是有关网络扫描管理软件Network Radar for Mac怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Network Radar for Mac是Mac平台上一款能够为您进行网络...
    99+
    2023-06-06
  • jQuery怎么自动完成组件
    小编给大家分享一下jQuery怎么自动完成组件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!jquery是什么jquery是一个简洁而快速的JavaScript库...
    99+
    2023-06-14
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作