Spring的component-scan

本文详细介绍了Spring框架中@ComponentScan注解的使用方法,包括basePackages属性的作用、use-default-filters属性的功能、以及如何通过include-filter和exclude-filter进行自定义的类筛选。

base-package  

1、basePackages

扫描basePackage制定包(及其子包)下的类,以尝试创建bean。将带有@Repository,@Service@Controller@Component的都创建一个bean

注解@ComponentScanbasePackages属性的默认值是注解@ComponentScan所在类所处的包。


2use-default-filters属性

是来控制是否要使用默认的过滤器的。

默认的过滤器会去解析base-package下的含有@Component注解的类作为bean

其中@Repository, @Service, and @Controller都是@Component的子注解,故,默认的过滤器会将它们全部解析成bean


3include-filterexclude-filter

自定义包含哪些注解和排除哪些注解。其中,exclude-filter优先级高于include-filter



4component-scan会创建一个bean,不管这个beanid是默认的)是否已经在xml中通过bean标签创建了。只是不一定会保留到beanFactory里(因为如果已经有了就会抛弃调后创建的)。



还可参见

http://www.cnblogs.com/kevin-yuan/p/5068448.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、付费专栏及课程。

余额充值