Spring基于管理之扫描注解升级版

本文介绍了如何在Spring Boot应用中,通过`context:exclude-filter`精确控制组件扫描,包括基于注解和类型进行排除,以及如何使用`context:include-filter`进行包含扫描。重点讲解了如何仅扫描特定类或排除控制器组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

13.2.1排除扫描 (除了他其余的全部扫描)

<!--
扫描组件
base-packge注意:以包的形式进行扫描 建议以描述的越精确越好,并包含了所有要扫描的注解 扫描的速度就越快

context:exclude-filter:排除扫描
type:设置排除扫描的方式
type:annotation/assignable
annotation:根据注解的类型进行排除,expression中需要设置排除的注解的全类名
assignable:根据类的类型进行排除,expression中需要设置排除的类的全类名
-->
    <context:component-scan base-package="com.atguigu.spring">
        <!--        通过注解(expression)排除扫描-->
<!--        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
        <!--        通过类型(expression)排除扫描-->
        <context:exclude-filter type="assignable" expression="com.atguigu.spring.controller.UserController"/>
    </context:component-scan>

13.2.2包含扫描 (只扫描我选择的)

<!--
context:include-filter:包含扫描
注意:需要在context:component-scan标签中设置 use-default-filters="false"
use-default-filters="true" (默认)为true则所设置的包下都需要扫描,此时可以使用排除扫描
use-default-filters="fals" 为true则所设置的包下都不需要扫描,此时可以使用包含扫描
-->
    <context:component-scan base-package="com.atguigu.spring" use-default-filters="false">
        <!--        通过注解(expression)排除扫描-->
<!--        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
        <!--        通过类型(expression)排除扫描-->
<!--        <context:exclude-filter type="assignable" expression="com.atguigu.spring.controller.UserController"/>-->
            </context:component-scan>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值