版权声明:本站所提供的文章资讯、软件资源、素材源码等内容均为本作者提供、网友推荐、互联网整理而来(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考,如有侵犯您的版权,请联系我,本作者将在三个工作日内改正。 https://blog.csdn.net/weixin_42323802/article/details/83473160
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..(…))”);
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="accountService" class="com.baidu.serviceImpl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="com.baidu.daoImpl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>-->
<!-- 引入dataSource 的资源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置事务管理 ,需要tx 、aop 命名空间约束 , tranactionManager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- isolation :事务隔绝等级
read-only :只读,对增删改false ,对查询使用true
propagation: 事物的传播行为,默认是required ,SUPPORTS用于增删改;
no-rollback-for: 指定哪种异常不回滚;rollback-for指定哪种异常回滚
timeout: 超时,默认-1永不超时;
-->
<tx:advice id="interceptor"><!-- interceptor -->
<tx:attributes>
<tx:method name="*"/> <!-- name 事务方法的名字 -->
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="one" expression="execution(* com.baidu.serviceImpl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="interceptor" pointcut-ref="one"></aop:advisor>
</aop:config>
<!-- 注意 advice-ref 、pointcut-ref引用指向name的正确性 -->
daoImpl 如下;
/**
* @auther SyntacticSugar
* @data 2018/10/28 0028上午 9:22
*/
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
/**
* 继承JdbcDaoSupport 来解决jdbcTemplate
* @param sourceName
* @return
*/
@Override
public Account queryAccountByName(String sourceName) {
String sql="select * from account where name =?";
Account account = getJdbcTemplate().queryForObject(sql, new BeanPropertyRowMapper<>(Account.class), sourceName);
return account;
}
@Override
public void updataAccountByAccount(Account source) {
String sql="update account set money=? where name =?";
getJdbcTemplate().update(sql,source.getMoney(), source.getName());
}
}
测试类;
/**
* @auther SyntacticSugar
* @data 2018/10/28 0028上午 9:35
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class AccountTest {
// 然后把 service 在注入 autoWired
@Autowired
private IAccountService accountService;
// 调用方法
@Test
public void transfer() {
accountService.transfer("aaa", "bbb", 2000f);
}
}
测试下;