在使用 Spring 的事务注解管理事务、使用 AspectJ 的 AOP 配置管理事务时运行代码报错。
大致报错信息如下:
- “通配符的匹配很全面, 但无法找到元素 ‘tx:annotation-driven’ 的声明”
- 加载 applicationContext 失败
- …
原因:加入相关配置后可能自动导入的约束错误、重复、缺少等都有可能造成报错。
解决:复制如下头部替换掉原来的即可(包含了两种配置方式的约束)
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">