spring-config.xml的annotation-driven详解

本文介绍Spring MVC中注解的支持方式,包括通用注解配置、MVC注解支持及事务和定时任务注解的使用。通过<context:annotation-config/>、<mvc:annotation-driven/>等简化配置,实现自动注入、控制器注解等功能。

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

使用spring mvc时,经常有这个注解annotation-driven,意思是支持注解。
例:
< context:annotation-config/>
支持配置注解
向 Spring 容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。
比如支持以下注解:
@Autowired注解,相当于在 Spring 容器中声明AutowiredAnnotationBeanPostProcessor

@ Resource 、@ PostConstruct、@ PreDestroy等注解,相当于声明CommonAnnotationBeanPostProcessor的Bean。
@PersistenceContext注解,相当于声明PersistenceAnnotationBeanPostProcessor的Bean。
@Required的注解,相当于声明RequiredAnnotationBeanPostProcessor的Bean。
这些注解是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供< context:annotation-config/>的简化配置方式,自动帮你完成声明。
不过,我们使用注解一般都会配置扫描包路径选项
< context:component-scan base-package=”XX.XX”/>
该配置项其实也包含了自动注入上述processor的功能,因此当使用 < context:component-scan/> 后,就可以将 < context:annotation-config/> 移除了。

< mvc:annotation-driven/>
支持MVC注解,在controller中使用mvc的各种注解。
自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
比如支持以下注解:
@NumberFormatannotation ,@DateTimeFormat ,@Valid
读写JSON的支持(Jackson)的@JsonIgnore
读写XML的支持(JAXB)

< tx:annotation-driven transaction-manager=“txManager”/>
支持事务注解
指定使用txManager事务管理器。

< task:annotation-driven executor=“executor” scheduler=“scheduler” />
支持定时任务注解

参考文章:
https://www.cnblogs.com/dreamroute/p/4493346.html

<think>我们正在讨论Spring XML配置文件中的常见标签及其作用。用户的问题集中在`beans`、`task`、`context`、`component-scan`等标签上。根据提供的引用,我们可以总结如下: 1. **`<beans>` 标签**: -Spring配置文件的根元素,用于定义Spring容器管理的所有Bean。 - 通过XML命名空间(如`xmlns`)引入其他Spring模块的标签定义。 2. **`<context:component-scan>` 标签**: - 用于自动扫描指定包路径下的类,将带有`@Component`、`@Service`、`@Controller`、`@Repository`等注解的类注册为Spring Bean。 - 属性`base-package`指定扫描的包路径,多个包可以用逗号分隔(引用[1]和引用[2])。 3. **`<context:annotation-config>` 标签**: - 用于激活Spring的注解驱动功能,如`@Autowired`、`@Resource`、`@PostConstruct`等(引用[3])。 - 注意:当配置了`<context:component-scan>`时,通常不需要再单独配置`<context:annotation-config>`,因为扫描已经包含了注解驱动的功能。 4. **`<task>` 标签**: - 属于Spring框架中任务调度(Task Execution and Scheduling)模块的命名空间。 - 用于配置异步任务执行、任务调度(定时任务)等,例如: - `<task:executor>`:配置线程池。 - `<task:scheduler>`:配置任务调度器。 - `<task:annotation-driven>`:启用基于注解的任务调度(如`@Scheduled`)。 5. **其他常见标签**: - **`<bean>`**:在`<beans>`内部使用,用于定义一个具体的Bean及其依赖。 - **`<import>`**:用于导入其他Spring配置文件。 现在,我们可以组织答案如下:</think>### Spring XML配置文件核心标签详解 #### 1. **`<beans>` 根标签** - **作用**:定义Spring配置文件的根元素,声明XML命名空间和Schema约束 - **关键属性**: ```xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="..." <!-- 指定XML Schema验证位置 --> ``` - **功能**: - 容器管理的所有Bean定义必须包含在此标签内 - 通过命名空间引入其他模块(如`xmlns:context`)[^3] #### 2. **`<context:component-scan>` 组件扫描** - **作用**:自动扫描并注册带有特定注解的类为Spring Bean - **核心属性**: ```xml base-package="com.example" <!-- 扫描的包路径(支持逗号分隔多包) --> ``` - **扫描注解类型**: | 注解 | 说明 | |------|------| | `@Component` | 通用组件 | | `@Service` | 服务层组件 | | `@Controller` | Web控制层 | | `@Repository` | 数据访问层 | - **工作流程**: 1. 扫描指定包下的类文件 2. 解析带有Spring注解的类 3. 生成`BeanDefinition`注册到容器 4. 容器初始化时创建实例[^1] #### 3. **`<context:annotation-config>` 注解驱动** - **作用**:激活Spring的注解处理能力 - **支持注解**: - 依赖注入:`@Autowired`, `@Resource` - 生命周期:`@PostConstruct`, `@PreDestroy` - 配置注解:`@Configuration`, `@Bean` - **注意**:当使用`<context:component-scan>`时,此标签可省略(扫描已包含注解驱动功能)[^3] #### 4. **`<task>` 任务调度标签**(需声明`xmlns:task`命名空间) - **核心子标签**: ```xml <!-- 启用异步任务执行 --> <task:executor id="threadPool" pool-size="5-10" queue-capacity="100"/> <!-- 启用定时任务注解 --> <task:annotation-driven executor="threadPool"/> ``` - **功能**: - 支持`@Async`异步方法调用 - 支持`@Scheduled`定时任务调度 - 线程池配置管理 #### 5. **其他关键标签** | 标签 | 功能 | 示例 | |------|------|------| | `<bean>` | 手动定义Bean | `<bean id="user" class="com.User"/>` | | `<property>` | 依赖注入 | `<property name="service" ref="userService"/>` | | `<import>` | 导入其他配置 | `<import resource="database-config.xml"/>` | | `<aop:aspectj-autoproxy>` | AOP注解支持 | 启用`@Aspect`切面处理 | #### 配置示例 ```xml <?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" xmlns:task="http://www.springframework.org/schema/task" 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 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!-- 注解驱动 --> <context:annotation-config/> <!-- 组件扫描 --> <context:component-scan base-package="com.example.service, com.example.dao"/> <!-- 任务调度配置 --> <task:executor id="taskExecutor" pool-size="10"/> <task:annotation-driven executor="taskExecutor"/> <!-- 手动定义的Bean --> <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"> <property name="jdbcUrl" value="jdbc:mysql://localhost/db"/> </bean> </beans> ``` > 💡 **最佳实践**: > 1. 优先使用`<context:component-scan>`+注解替代XML显式配置 > 2. 大型项目推荐`@Configuration` Java配置取代XML > 3. 定时任务需配合`@EnableScheduling`注解使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值