目录
Spring 组件扫描指南
一、环境搭建与基础配置
添加依赖项
<!-- pom.xml -->
<dependencies>
<!-- 核心容器 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.8</version>
</dependency>
<!-- AOP支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.8</version>
</dependency>
</dependencies>
配置命名空间
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置内容区 -->
</beans>
二、组件扫描核心配置
基础扫描设置
<!-- 扫描指定包及其子包 -->
<context:component-scan base-package="com.hspedu.spring.component"/>
主要功能:
- 自动扫描com.hspedu.spring.component包及其子包
- 识别标准注解类:@Component、@Controller、@Service、@Repository
- 自动注册为Spring Bean
扫描路径详解
<!-- 不同包路径示例 -->
<context:component-scan base-package="com.hspedu.spring"/> <!-- 当前包及子包 -->
<context:component-scan base-package="com.hspedu.spring.*"/> <!-- 仅子包 -->
<context:component-scan base-package="com.hspedu.spring.component.service"/> <!-- 特定子包 -->
三、过滤控制:精确管理扫描内容
排除特定组件
<context:component-scan base-package="com.hspedu.spring.component">
<!-- 排除@Service注解的类 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service"/>
</context:component-scan>
过滤类型说明
过滤类型(type) | 说明 | 示例表达式 |
---|---|---|
annotation | 按注解排除 | org.springframework.stereotype.Service |
assignable | 按类/接口排除 | com.example.MySpecialClass |
regex | 按正则表达式排除 | .*Test$ |
aspectj | 使用AspectJ表达式排除 | com.example..*Service |
包含特定组件
<context:component-scan base-package="com.hspedu.spring.component"
use-default-filters="false">
<!-- 仅包含@Controller和@Service注解的类 -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service"/>
</context:component-scan>
参数说明:
use-default-filters="false"
:关闭默认的@Component扫描- 可添加多个
<context:include-filter>
条件
按名称模式过滤
<context:component-scan base-package="com.hspedu.spring.component"
resource-pattern="User*.class"/>
仅扫描类名以"User"开头的组件,支持Ant风格路径匹配(如*Service.class, *Dao.class)
四、Bean命名规则与自定义
默认命名规则
@Component
public class UserService {} // Bean ID = "userService"
@Controller
public class OrderAction {} // Bean ID = "orderAction"
规则:将类名首字母小写
示例:UserService → userService
自定义Bean ID
// 自定义Bean ID
@Controller("userAction01") // 等价于 @Controller(value="userAction01")
public class UserAction {
// ...
}
@Service("paymentService")
public class PaymentServiceImpl {
// ...
}
五、Spring容器工作机制解析
注解处理流程
容器启动 → 扫描base-package → 检查注解 → 是 → 注册为BeanDefinition → 创建Bean实例 → 依赖注入 → 初始化完成
↓
否 → 跳过
注解的本质
- @Controller、@Service本质都是@Component
- Spring仅将注解视为组件标记,不检查其实际含义
// 从容器角度看:
@Component == @Controller == @Service // 均为需要创建Bean的标记
六、最佳实践与常见问题
分层架构推荐配置
<!-- 推荐的多层扫描配置示例 -->
<context:component-scan base-package="com.hspedu">
<!-- 控制层组件扫描 -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
<!-- 服务层组件扫描 -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service"/>
<!-- 排除测试类 -->
<context:exclude-filter type="regex"
expression=".*Test"/>
</context:component-scan>
常见问题解决方案
Bean扫描不到问题排查:
- 检查base-package路径是否准确
- 确认组件未被过滤规则排除
- 验证目标类是否添加了相应注解
过滤规则不生效处理方法:
<!-- 注意:必须先设置use-default-filters="false" -->
<context:component-scan base-package="com.hspedu" use-default-filters="false">
<context:include-filter .../>
<context:exclude-filter .../>
</context:component-scan>
七、实际案例演示
项目目录结构:
src/main/java
└── com
└── hspedu
└── spring
├── component
│ ├── controller
│ │ └── UserController.java
│ ├── service
│ │ ├── UserService.java
│ │ └── OrderService.java
│ └── util
│ └── DateUtils.java
└── MainApp.java
分层扫描配置示例:
<!-- 业务层和控制层扫描配置 -->
<context:component-scan base-package="com.hspedu.spring.component"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service"/>
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
八、核心总结
基础配置:
<context:component-scan base-package="你的项目包路径"/>
核心特性:
- 过滤机制:
- 排除:
<context:exclude-filter>
- 包含:
<context:include-filter>
+use-default-filters="false"
- 排除:
- Bean命名规则:
- 默认:类名首字母小写
- 自定义:
@Component("自定义名称")
注意事项:
- 开发注解仅作标识,实际由
@Component
实现扫描 - 包扫描自动包含所有子包
- 支持多重过滤规则组合
最佳实践建议:
- 采用标准分层结构(controller/service/repository)
- 按业务层级配置扫描规则
- 为关键组件设置明确名称
- 生产环境务必排除测试类
通过本教程,您已掌握Spring组件扫描的核心配置与应用技巧!