【SpringMVC】<context:include-filter>和<context:exclude-filter>使用时要注意的地方

本文详细解析了SpringMVC中&lt;context:component-scan&gt;的使用方法,特别是如何通过&lt;context:include-filter&gt;和&lt;context:exclude-filter&gt;精确控制扫描范围,避免事务冲突,确保控制器与业务逻辑的清晰分离。

在Spring MVC中的配置中一般会遇到这两个标签,作为<context:component-scan>的子标签出现。

但在使用时要注意一下几点:

1.在很多配置中一般都会吧Spring-common.xml和Spring-MVC.xml进行分开配置,这种配置就行各施其职一样,显得特别清晰。

在Spring-MVC.xml中只对@Controller进行扫描就可,作为一个控制器,其他的事情不做。

在Spring-common.xml中只对一些事务逻辑的注解扫描。

2.现在给定一个项目包的机构:

com.fq.controlller

com.fq.service

就先给定这两个包机构

(1)在Spring-MVC.xml中有以下配置:

 

 

 

<!-- 扫描@Controller注解 -->
<context:component-scan base-package="com.fq.controller">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

可以看出要把最终的包写上,而不能这样写base-package=”com.fq”。这种写法对于include-filter来讲它都会扫描,而不是仅仅扫描@Controller。哈哈哈,这点需要注意。他一般会导致一个常见的错误,那就是事务不起作用,补救的方法是添加use-default-filters=”false”。

(2)在Spring-common.xml中有如下配置:

<!-- 配置扫描注解,不扫描@Controller注解 -->
<context:component-scan base-package="com.fq">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

可以看到,他是要扫描com.fq包下的所有子类,不包含@Controller。对于exculude-filter不存在包不精确后都进行扫描的问题。

转载于:https://www.cnblogs.com/haoke/p/4604883.html

### Spring 中 `context:component-scan` 配置详解 `<context:component-scan>` 是 Spring 框架中用于自动扫描并注册组件的核心配置标签。通过设置 `base-package` 属性,Spring 会扫描指定包及其子包下的类,并根据注解(如 `@Component`, `@Service`, `@Controller`, `@Repository` 等)将符合条件的类注册为 Spring 容器中的 Bean[^1]。 #### 基本用法 以下是一个典型的 `<context:component-scan>` 配置示例: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.example"/> </beans> ``` 上述配置表示 Spring 将扫描 `com.example` 包及其所有子包,查找带有特定注解的类并将其注册为 Spring Bean[^3]。 #### 排除特定类型的类 如果需要排除某些类型的类(例如不希望扫描到的 `@Service` 类),可以使用 `<context:exclude-filter>` 子标签。例如: ```xml <context:component-scan base-package="com.springmvc"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> ``` 此配置表示在扫描 `com.springmvc` 包,排除带有 `@Service` 注解的类[^1]。 #### 常见问题及解决方法 1. **路径配置错误** 如果切面或注解类未被正确扫描,可能是由于 `base-package` 的路径配置错误。确保 `base-package` 覆盖了所有需要扫描的类所在的包及其子包[^5]。 2. **切面不生效** 切面类未被正确扫描可能导致切面逻辑不生效。通常需要确保切面类所在的包已被 `base-package` 覆盖。例如,假设项目结构如下: ``` com.zhang.spring ├── aspect │ └── LoggingAspect.java ├── service │ └── MyService.java └── controller └── MyController.java ``` 此,`<context:component-scan base-package="com.zhang.spring"/>` 应覆盖 `aspect`, `service`, `controller` 包,否则切面可能无法正常工作[^5]。 3. **与其他配置冲突** `<context:annotation-config>` `<context:component-scan>` 的功能有部分重叠,但两者的作用范围不同。`<context:component-scan>` 已经隐式包含了 `<context:annotation-config>` 的功能,因此通常只需要配置 `<context:component-scan>` 即可[^2]。 4. **BeanPostProcessor 未加载** 在某些情况下,Spring 容器可能未正确加载 `CommonAnnotationBeanPostProcessor` 等后置处理器,导致注解功能失效。可以通过检查源码确认是否正确加载了相关处理器[^4]。 #### 示例代码 以下是一个完整的 XML 配置示例,展示如何结合 `<context:component-scan>` `<context:exclude-filter>` 使用: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.example"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> </beans> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值