解决错误:通配符的匹配很全面, 但无法找到元素 'tx:annotation-driven'

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21004057/article/details/79468381

出现这种错误,解决办法是声明式事务的头部声明没有被导入或者缺少jar包导致。关于头部声明没有被导入可能是缺少或者是版本不对。

1.统一去掉版本号,默认会自动添加当前版本。类似下面

<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:tx="http://www.springframework.org/schema/tx"
       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/tx
        http://www.springframework.org/schema/spring-tx.xsd"
        >

2.查看webapp下的lib目录是不是把声明式事务的四个包导入。

appolliance.jar

aspectjrt.jar

aspectjweaver.jar

cglib-nodep.jar

3.applicationContext.xml配置文件中需要添加如下代码。

 <!--配置事务管理-->
    <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!--开启注解事务-->
    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
    <!--  允许事务注解在文件中使用-->
    <context:annotation-config/>

4.在service层声明transactional注解。

@Transactional
public class UserServiceImpl implements IUserService{

猜你喜欢

转载自blog.csdn.net/qq_21004057/article/details/79468381