【Spring AOP】学习记录(二)
来源:互联网 发布:三角形测试用例java 编辑:程序博客网 时间:2024/05/21 12:40
国庆过得好像有点忙,是不是生活过得简单了,就有时间享福了?当然国庆假期也抽点时间来学习学习,不然赶不上年轻人的脚步
学啥
今天学习了一下aop的事务管理,没分析内部,估计只不过对使用进行了封装,让我们使用更加简便而已
功能
- 可以通过切面编程对指定的进行事务管理
- 可以控制异常的类型才进行回滚
- 可以控制事务的timeout
Propagation取值:
例子1(xml)
直接看例子再分析分析
<?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" 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"> <aop:config> <!--定义切入点1,要对指定的进行管理事物--> <aop:pointcut id="defaultServiceOperation" expression="execution(* x.y.service.*Service.*(..))"/> <!--定义切入点2,要对指定的进行管理事物--> <aop:pointcut id="noTxServiceOperation" expression="execution(* x.y.service.ddl.DefaultDdlManager.*(..))"/> <!--对上面的切入点1采用defaultTxAdvice实例进行管理--> <aop:advisor pointcut-ref="defaultServiceOperation" advice-ref="defaultTxAdvice"/> <!--对上面的切入点2采用noTxAdvice实例进行管理--> <aop:advisor pointcut-ref="noTxServiceOperation" advice-ref="noTxAdvice"/> </aop:config> <!-- this bean will be transactional (see the 'defaultServiceOperation' pointcut) --> <bean id="fooService" class="x.y.service.DefaultFooService"/> <!-- this bean will also be transactional, but with totally different transactional settings --> <bean id="anotherFooService" class="x.y.service.ddl.DefaultDdlManager"/> <!--对方法名get开始的,只允许读,不允许修改等--> <tx:advice id="defaultTxAdvice"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!--在无transaction状态下执行;如果当前已有transaction,则抛出异常IllegalTransactionStateException。--> <tx:advice id="noTxAdvice"> <tx:attributes> <tx:method name="*" propagation="NEVER"/> </tx:attributes> </tx:advice> <!-- other transaction infrastructure beans such as a PlatformTransactionManager omitted... --></beans>
分析说明
其实还是利用了AOP那一套,有稍微一点点不一样
- tx:advice 事务控制点
- transaction-manager 事务管理者
例子2(注解)
直接看例子再分析分析
<?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" 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"> <!--允许注解管理事务--> <tx:annotation-driven transaction-manager="txManager" order="200"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/> <property name="username" value="scott"/> <property name="password" value="tiger"/> </bean> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean></beans>
java代码
@Transactional(readOnly = true)public class DefaultFooService implements FooService { public Foo getFoo(String fooName) { // do something } // these settings have precedence for this method @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW) public void updateFoo(Foo foo) { // do something }}
重点
<tx:annotation-driven transaction-manager="txManager" order="200"/>
支持事务注解
0 0
- 【Spring AOP】学习记录(二)
- Spring学习记录-AOP
- Spring AOP学习记录
- spring aop学习记录
- 【Spring AOP】学习记录(一)
- Spring学习--aop学习记录
- Spring 学习笔记 (二)--AOP
- Spring源码学习(二)------ AOP
- Spring源码学习(二)------ AOP
- Spring源码学习(二)------ AOP
- Spring学习笔记(二)AOP入门
- Spring源码学习(二)------ AOP
- Spring源码学习(二)------ AOP
- Spring源码学习(二)------ AOP
- Spring AOP学习(二)--AspectJ
- AOP学习笔记(二)——Spring AOP
- spring学习之AOP(二)
- Spring AOP (二)
- JZOJ 4810. 道路规划
- 【CodeForces 676】C - Vasya and String
- exit(0)与exit(1)、return区别
- Unity
- mysql索引问题
- 【Spring AOP】学习记录(二)
- javax.servlet.jsp.JspException cannot be resolved to a type
- 优先读者的读者/写者问题的算法设计
- CancelIO的作用:防止为发送的数据丢失
- JZOJ 4810 【NOIP2016提高A组五校联考1】道路规划
- Java的反射基本概念、语法和应用
- 剑指offer_整数中1出现的次数
- Linux嵌入式开发常见面试题
- 同义词总结