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>