天天看點

"通配符的比對很全面, 但無法找到元素 'tx:annotation-driven' 的聲明"2018.10.28,Spring5.0.7事務TransactionManager的xml配置

spring開啟事務配置tx,aop時候測試,報出一大堆錯誤;

其中有:

①"通配符的比對很全面, 但無法找到元素 ‘tx:annotation-driven’ 的聲明"

②URI必須偶數個

③加載applicationContext失敗

④找不到 tx:advice

是因為xml頭部各種原因有可能重複,有可能缺少限制、、、,直接copy一份頭部替換掉原來的頭部就行了;

<?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">
           

事務配置如下:

(1)daoImpl 繼承了 JdbcDaoSupport 來擷取 JdbcTemplate ,簡化了applicationContext.xml 的 bean 配置;

(2)事務需要tx、aop命名空間限制配置,使用到了切入點表達式通知;@Pointcut(“execution(* com.baidu.service.impl..(…))”);

配置事務:

<!--  配置事務  transactionManager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--向事務中注入源資料-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
           

配置 tx 事務的屬性;

<!--  配置 事務的屬性 、find開頭的方法隻讀事務,其他 -->
    <tx:advice id="interceptor" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
           

配置 aop 事務的切面‘’;

<!--配置事務的切面-->
    <aop:config>
        <!-- 設定 pointcut   表達式,并且把定義好事務屬性的應用到切入點  -->
        <aop:pointcut id="pt1" expression="execution(* com.baidu.serviceImpl.AccountServiceImpl.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="interceptor" pointcut-ref="pt1"/>
    </aop:config>
           

繼續閱讀