Spring_four
2020-12-13 04:52
标签:mono 编程 activemq strong toc initial 解决方案 资源 分层 坐标xml 删除AccountServiceTest测试类上的@Qualifier的注解,不产生代理对象 此时事务没有控制住 applicationContext.xml 可以使用【前置通知】、【后置通知】、【异常通知】、【最终通知】 坐标 配置AccountServiceImpl.java的注解 配置AccountDaoImpl.java的注解 配置ConnectionUtils.java的注解 配置TransactionManager.java的注解 配置spring容器 配置applicationContext.xml text 但是发现,抛出异常。 因为注解的方式执行顺序是【前置通知】、【最终通知】、【异常通知】/【后置通知】 我们需要使用环绕通知解决问题。 配置TransactionManager.java 它是spring框架中提供的一个对象,是对原始Jdbc API对象的简单封装。spring框架为我们提供了很多的操作模板类。 操作关系型数据的: JdbcTemplate (操作JDBC,操作数据库) HibernateTemplate (操作hibernate,操作数据库) 操作nosql数据库的: RedisTemplate(操作Redis,非关系型数据库) 操作消息队列(MQ)的: JmsTemplate (操作ActiveMQ,消息队列) * 短信平台 * 邮件平台 操作索引库的:ElasticSearchTemplate(操作ElasticSearch,全文检索) 我们今天的主角在spring-jdbc-5.0.2.RELEASE.jar中,我们在导包的时候,除了要导入这个jar包外,还需要导入一个spring-tx-5.0.2.RELEASE.jar(它是和事务相关的)。 坐标xml 创建类Account.java 创建类JdbcTemplateDemo1.java 使用spring提供的数据源Spring_four
基于XML的AOP实现事务控制
spring-context spring-test commons-dbutils mysql-connector-java c3p0 junit aspectjweaver 
/*** 使用Junit单元测试:测试我们的配置*/@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:applicationContext.xml")public class AccountServiceTest { @Autowired private AccountService as; @Test public void testTransfer(){ as.transfer("aaa","bbb",100f); }}
【配置文件】添加spring的aop
xml version="1.0" encoding="UTF-8"?> xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
【注解】添加spring的aop
spring-context spring-test commons-dbutils mysql-connector-java c3p0 junit aspectjweaver
@Servicepublic class AccountServiceImpl implements AccountService { @Autowired private AccountDao accountDao;}
@Repositorypublic class AccountDaoImpl implements AccountDao { @Autowired private QueryRunner runner; @Autowired private ConnectionUtils connectionUtils;}
@Componentpublic class ConnectionUtils { private ThreadLocalConnection> tl = new ThreadLocalConnection>(); @Autowired private DataSource dataSource;}
@Component@Aspect // 切面public class TransactionManager { @Autowired private ConnectionUtils connectionUtils; @Pointcut(value = "execution(* com.it.service..*.*(..))") // 切入点 public void pc(){}; /** * 开启事务 */ @Before(value = "pc()") public void beginTransaction(){ try { System.out.println("前置通知"); connectionUtils.getThreadConnection().setAutoCommit(false); }catch (Exception e){ e.printStackTrace(); } } /** * 提交事务 */ @AfterReturning(value = "pc()") public void commit(){ try { System.out.println("后置通知"); connectionUtils.getThreadConnection().commit(); }catch (Exception e){ e.printStackTrace(); } } /** * 回滚事务 */ @AfterThrowing(value = "pc()") public void rollback(){ try { System.out.println("异常通知"); connectionUtils.getThreadConnection().rollback(); }catch (Exception e){ e.printStackTrace(); } } /** * 释放连接 */ @After(value = "pc()") public void release(){ try { System.out.println("最终通知"); connectionUtils.getThreadConnection().close();//把连接还回连接池中 connectionUtils.removeConnection(); }catch (Exception e){ e.printStackTrace(); } }}
xml version="1.0" encoding="UTF-8"?> xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:applicationContext.xml")public class AccountServiceTest { @Autowired private AccountService as; @Test public void testTransfer(){ as.transfer("aaa","bbb",100f); }}

@Component@Aspectpublic class TransactionManager { @Autowired private ConnectionUtils connectionUtils; @Pointcut(value = "execution(* com.it.service..*.*(..))") public void pc(){}; /** * 开启事务 */ //@Before(value = "pc()") public void beginTransaction(){ try { System.out.println("前置通知"); connectionUtils.getThreadConnection().setAutoCommit(false); }catch (Exception e){ e.printStackTrace(); } } /** * 提交事务 */ //@AfterReturning(value = "pc()") public void commit(){ try { System.out.println("后置通知"); connectionUtils.getThreadConnection().commit(); }catch (Exception e){ e.printStackTrace(); } } /** * 回滚事务 */ //@AfterThrowing(value = "pc()") public void rollback(){ try { System.out.println("异常通知"); connectionUtils.getThreadConnection().rollback(); }catch (Exception e){ e.printStackTrace(); } } /** * 释放连接 */ //@After(value = "pc()") public void release(){ try { System.out.println("最终通知"); connectionUtils.getThreadConnection().close();//把连接还回连接池中 connectionUtils.removeConnection(); }catch (Exception e){ e.printStackTrace(); } } @Around(value="pc()") public Object around(ProceedingJoinPoint joinPoint){ Object returnValue = null; try { this.beginTransaction(); // 开启事务 returnValue = joinPoint.proceed(joinPoint.getArgs()); this.commit(); // 提交事务 } catch (Throwable throwable) { throwable.printStackTrace(); this.rollback(); // 回滚事务 } finally { this.release(); // 释放资源 } return returnValue; }}
Spring中的JdbcTemplate
1\dbcTemplate概述
spring-context spring-jdbc spring-tx mysql-connector-java
/*** 账户的实体类*/public class Account implements Serializable { private Integer id; private String name; private Float money;}
/*** JdbcTemplate的最基本用法*/public class JdbcTemplateDemo1 { public static void main(String[] args) { //准备数据源:spring的内置数据源 DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setUrl("jdbc:mysql://localhost:3306/itcastspring"); ds.setUsername("root");