栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

@componentScan

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

@componentScan

spring注解目录

sping注解包括了许多的spring相关注解的解释和应用。


目录

概述

excludeFilters

includeFilters

ComponentScans


概述

@ComponentScan主要是扫描包下面的组件。

 @ComponentScan告诉他一个包com.demo,他就会到com.demo下面去找所有的组件。

然后还可以,里面有一些spring的默认包。

excludeFilters

然后想要指定扫描一些组件呢?使观察到@ComponentScan下面有如下方法

ComponentScan.Filter[] includeFilters() default {};

ComponentScan.Filter[] excludeFilters() default {};

可以除去一些不想扫描的组件,例如除去controller注解

@ComponentScan(value = "com.demo",excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
})

然后可以看见,controller注解被排除在外了。

花括号里面可以写,更多的排除方式用逗号隔开。

includeFilters

includeFilters主要是排除相关的组件,告诉includeFilters要排除那些组件就可以了吗?由于spring有默认的扫描方式,即我所有的组件都要扫描,所以只是告诉includeFilters要排除那些还是会默认扫描所有,因此禁用掉全局扫描使用useDefaultFilters = false。

@ComponentScan(value = "com.demo",includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
},useDefaultFilters = false)

 这样,就只扫描到了controller组件,进入了IOC容器。

ComponentScans

要一次性添加多个componentScan,可以使用ComponentScans。

@ComponentScans(
        value = {
                @ComponentScan(value = "com.demo",includeFilters = {
                        @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
                },useDefaultFilters = false)
        }
)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/489878.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号