`

spring声明式事务策略 aop拦截-学习笔记

阅读更多

 

声明式事务管理:

Spring提供了声明式事务管理。是通过Spring AOP实现的。

Spring中进行事务管理的通常方式是利用AOP(面向切片编程)的方式,为普通java类封装事务控制,它是通过动态代理实现的,由于接口是延迟实例化的,spring的在这段时间内通过拦截器,加载事务切片。原理就是这样的,可以参考JDK动态代理实例。

Spring中进行事务控制:

动态代理的一个重要特征是,它是针对接口的,所以我们的DAO要通过动态代理来让spring接管事务,就必须在dao前面抽象出一个接口,当然如果没有这样的接口,那么spring会使用CGLIB来决解问题,但这不是spring推荐的方式。

大多数spring用户选择声明式事务管理。这是最少影响应用代码的选择,因而这是和非侵入性的轻量级容器的观念是一致的。

从考虑EJB CMT和spring声明式事务管理的相似以及不同之处出发是很有益的。他们的基本方法是相似的:都可以指定事务管理到单独的方法;如果需要可以在事务上下文调用setRollbackOnly()方法。不同之处如下:

1、不像EJB CMT绑定在JTA上,spring声明式事务管理可以在任何环境下使用。只需要更改配置文件,它就可以和JDBC、JDO、Hibernate或其他的事务机制一起工作。

2、spring可以使声明式事务管理应用到普通的Java对象,不仅仅是特殊的类,如EJB。

3、spring提供声明式回滚规则:EJB诶呦地域的特征,回滚可以声明式控制,不仅仅是编程式的。

4、spring允许你通过AOP定制事务行为。如:如果需要,可以在事务回滚中插入定制的行为。也可以增加任意的通知,就像事务通知一样。使用EJB CMT,除了使用setRollbackOnly(),你没有办法能够影响容器的事务管理。

5、spring不提供高端应用服务器的跨越远程调用的事务上下文传播。如果你需要这些特征,推荐你使用EJB。然而,不要轻易使用这些特性。通常我们并不希望事务跨越远程调用。


回滚规则的概念:他们使得我们可以指定哪些异常应该发起自动回滚。我们在配置文件中,而不是Java代码中,以声明的方式指定。因此,虽然我们仍然可以编程调用TransactionStatus对象的setRollbackOnly()方法来回滚当前事务,多数时候我们可以指定规则,如MyApplicationException应用导致回滚。这有明显的优点,业务对象不需要依赖事务基础设施。例如:通常不需要引入任何Spring API,事务或其他任何东西。

EJB的默认行为是遇到系统异常(通常是运行是异常),EJB容器自动回滚事务。EJB CMT遇到应用程序异常(除了java.rmi.RemoteExcepiton外的checked异常)是不需要自动回滚事务。虽然Spring声明式事务管理沿用EJB的约定(遇到unchecked异常自动回滚事务),但是这是可以定制的。

Spring声明式事务管理的性能要胜过EJB CMT。

 

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/context 
			http://www.springframework.org/schema/context/spring-context-2.5.xsd
			http://www.springframework.org/schema/aop 
			http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	
	<!-- 
		打开配置项,这个配置项是对@Aspectj这个注解进行支持 注解本身是不能干活的,注解之所以能干活是因为后面有处理器对其进行处理
		这个配置相当于我们将要使用的@Aspectj注解提供了解析的功能
	 -->
	<aop:aspectj-autoproxy />
	<context:component-scan base-package="com.sample.*"/>
	<bean id="apectBean" class="com.sample.service.MyInterceptor2"/>
	<aop:config>
		<aop:aspect id="asp" ref="apectBean">
			<aop:pointcut id="mycut" expression="execution (void com.sample.service.impl.PersonServiceBean*.*(String))"/>
			<aop:before pointcut-ref="mycut" method="doAccessCheck" arg-names="name" />
			<aop:after pointcut-ref="mycut" method="doAfter"/>
			<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
			<aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
			<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
		</aop:aspect>
	</aop:config>
	
	<aop:config>
		<aop:aspect id="myAOP" ref="check">
			<aop:pointcut id="target" expression="execution(* com.sample.service.Common.execute2(..))"/>
			<aop:before method="checkValidator" pointcut-ref="target"/>
			<aop:after method="addLog" pointcut-ref="target"/>
		</aop:aspect>
	</aop:config>
</beans>
 

 

 

 

分享到:
评论

相关推荐

    Spring5 框架 ---- AOP ---- 代码

    Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 ...

    spring-aop.jar各个版本

    spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...

    开发工具 spring-aop-4.3.6.RELEASE

    开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...

    spring-aop-5.2.0.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...

    使用Spring的声明式事务----AOP方式

    NULL 博文链接:https://tonl.iteye.com/blog/2093314

    spring-aop-5.0.8.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.0.8.RELEASE.jar; 赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom;...

    spring源码--AOP流程--笔记.docx

    aop分析笔记 个人总结所得 org.springframework.aop.framework.autoproxy

    spring-aop-5.0.10.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....

    spring-aop-5.3.10-API文档-中文版.zip

    赠送jar包:spring-aop-5.3.10.jar; 赠送原API文档:spring-aop-5.3.10-javadoc.jar; 赠送源代码:spring-aop-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.10.pom; 包含翻译后的API文档:spring...

    aopalliance-1.0.jar,org.springframework.aop-3.0.0.RELEASE.jar

    aopalliance-1.0.jar,org.springframework.aop-3.0.0.RELEASE.jar,org.springframework.jdbc-3.0.0.RELEASEorg.springframework.beans-3.0.0.RELEASE.jar等

    spring-aop-4.2.2.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-4.2.2.RELEASE.jar; 赠送原API文档:spring-aop-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-4.2.2.RELEASE.pom;...

    spring-aop-5.1.3.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.1.3.RELEASE.jar; 赠送原API文档:spring-aop-5.1.3.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.1.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.1.3.RELEASE.pom;...

    开发工具 aopalliance-1.0

    开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具...

    死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理(csdn)————程序.pdf

    死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理(csdn)————程序

    spring-aop-5.3.12-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.3.12.jar; 赠送原API文档:spring-aop-5.3.12-javadoc.jar; 赠送源代码:spring-aop-5.3.12-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.12.pom; 包含翻译后的API文档:spring...

    spring-aop-5.2.15.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.15.RELEASE.jar; 赠送原API文档:spring-aop-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.15.RELEASE....

    spring-aop-4.3.20.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-4.3.20.RELEASE.jar 赠送原API文档:spring-aop-4.3.20.RELEASE-javadoc.jar 赠送源代码:spring-aop-4.3.20.RELEASE-sources.jar 包含翻译后的API文档:spring-aop-4.3.20.RELEASE-...

    spring-aop-5.2.7.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.2.7.RELEASE.jar; 赠送原API文档:spring-aop-5.2.7.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.7.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.7.RELEASE.pom;...

    spring-aop-5.0.5.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.0.5.RELEASE.jar; 赠送原API文档:spring-aop-5.0.5.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.5.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.5.RELEASE.pom;...

    spring-aop-4.3.12.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-4.3.12.RELEASE.jar; 赠送原API文档:spring-aop-4.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.3.12.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-4.3.12.RELEASE....

Global site tag (gtag.js) - Google Analytics