- 浏览: 565132 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
a1641693970:
还不错,学习了
BeanUtils使用总结(二)LazyDynaBean -
zjfshowtime:
很好的办法,学习了。
ORA-28001: the password has expired -
ya654277yo:
哦,多谢分享
Apache整合Tomcat后get方式提交中文乱码问题解决 -
foolkeeper:
nice !!
jvm内存参数设定 -
tracy821:
谢谢了,找了好久才找到
关于Spring 声明式事务处理时,throws exception不回滚的问题
一、AOP 概念
Joinpoint:它定义在哪里加入你的逻辑功能,对于Spring AOP,Jointpoint指的就是Method。 Advice:特定的Jointpoint处运行的代码,对于Spring AOP 来讲,有Before advice、AfterreturningAdvice、ThrowAdvice、AroundAdvice(MethodInteceptor)等。 Pointcut:一组Joinpoint,就是说一个Advice可能在多个地方织入, Aspect:这个我一直迷惑,它实际是Advice和Pointcut的组合,但是Spring AOP 中的Advisor也是这样一个东西,但是Spring中为什么叫Advisor而不叫做Aspect。 Weaving:将Aspect加入到程序代码的过程,对于Spring AOP,由ProxyFactory或者ProxyFactoryBean负责织入动作。 Target:这个很容易理解,就是需要Aspect功能的对象。 Introduction:引入,就是向对象中加入新的属性或方法,一般是一个实例一个引用对象。当然如果不引入属性或者引入的属性做了线程安全性处理或者只读属性,则一个Class一个引用也是可以的(自己理解)。Per-class lifecycle or per-instance life cycle 二、AOP 种类 1、静态织入:指在编译时期就织入Aspect代码,AspectJ好像是这样做的。 2、动态织入:在运行时期织入,Spring AOP属于动态织入,动态织入又分静动两种,静则指织入过程只在第一次调用时执行;动则指根据代码动态运行的中间状态来决定如何操作,每次调用Target的时候都执行(性能较差)。 三、Spring AOP 代理原理 Spring AOP 是使用代理来完成的,Spring 会使用下面两种方式的其中一种来创建代理: 1、JDK动态代理,特点只能代理接口,性能相对较差,需要设定一组代理接口。 2、CGLIB 代理,可代理接口和类(final method除外),性能较高(生成字节码)。 四、Spring AOP 通知类型 1、BeforeAdvice:前置通知需实现MethodBeforeAdvice,但是该接口的Parent是BeforeAdvice,致于什么用处我想可能是扩展性需求的设计吧。或者Spring未来也并不局限于Method的JoinPoint(胡乱猜测)。BeforeAdvice可以修改目标的参数,也可以通过抛出异常来阻止目标运行。 2、AfterreturningAdvice:实现AfterreturningAdvice,我们无法修改方法的返回值,但是可以通过抛出异常阻止方法运行。 3、AroundAdvice:Spring 通过实现MethodInterceptor(aopalliance)来实现包围通知,最大特点是可以修改返回值,当然它在方法前后都加入了自己的逻辑代码,因此功能异常强大。通过MethodInvocation.proceed()来调用目标方法(甚至可以不调用)。 4、ThrowsAdvice:通过实现若干afterThrowing()来实现。 5、IntroductionInterceptor:Spring 的默认实现为DelegatingIntroductionInterceptor 五、Spring AOP Pointcut 以上只是Advice,如果不指定切入点,Spring 则使用所有可能的Jointpoint进行织入(当然如果你在Advice中进行方法检查除外)。因此切入点在AOP中扮演一个十分重要的角色。Spring 2.0 推荐使用AspectJ的Annocation的切入点表达式来定义切入点,或者使用<aop:xxx/>来定义AOP,这方面本篇不做考虑。 1、Pointcut:它是Spring AOP Pointcut的核心,定义了getClassFilter()和getMethodMatcher()两个方法。 2、ClassFilter:定义了matches(Class cls)一个方法。 3、MethodMatcher() 定义了matches(Method,Class),isRuntime(),matches(Mathod,Class,Object[])三个方法,如果isRuntime()返回true则表示为动态代理(实际是动态代理的动态代理),则调用第三个方法(每访问一次调用一次),否则调用第一个方法(并且只调用一次) 4、Spring AOP 静态切入点的几个实现。 ComposablePointcut 太复杂一个切入点无法表达就用这个,union MethodMatcher和ClassFilter或者intersection MethodMatcher、ClassFilter和Pointcut。为什么不实现union Pointcut? 而只能通过Pointcuts类对Pointcut进行union操作。 ControlFlowPointcut 想对程序的运行过程进行追踪就用这个 DynamicMatchMatcherPointcut 想用动态AOP 就用这个 JdkRegexpMethodPointcut 想使用正则表达式就用这个 Perl5RegexpMethodPointcut NameMatchMethodPointcut 想用方法名字来匹配就用这个 StaticMethodMatcherPointcut 静态切入点就用这个 没有人反对你直接实现Pointcut:)。 六、Spring AOP 中的Advisor其实就是Aspect 1、 PointcutAdvisor 其实一般使用DefaultPointcutAdvisor就足够了,给它Advice和Pointcut。 当然如果想少写那么几行代码也可以使用NameMatchMethodPointcutAdvisor,RegexpMethodPointcutAdvisor等。 更多Advisor可以查看API文档。 2、 IntroductionAdvisor 默认实现为DefaultIntroductionAdvisor。 七、AOP ProxyFactory 使用代码实现AOP 可使用ProxyFactory 声明式AOP 可使用ProxyFactoryBean ProxyFactoryBean 需要设定 target,interceptorNames(可以是Advice或者Advisor,注意顺序) 对接口代理需设置proxyInterfaces 八、自动代理 BeanNameAutoProxyCreator |
DefaultAdvisorAutoProxyCreator
- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
- <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
- <property name="transactionInterceptor" ref="transactionInterceptor"/>
- </bean>
- <bean id="customAdvisor" class="com.mycompany.MyAdvisor"/>
- <bean id="businessObject1" class="com.mycompany.BusinessObject1">
- <!-- Properties omitted -->
- </bean>
- <bean id="businessObject2" class="com.mycompany.BusinessObject2"/>
发表评论
-
Spring 多数据源声明式事务控制(PlatformTransactionManager )
2014-04-11 18:31 4655首先是TransactionManager的配置: ... -
Spring 动态数据源(AbstractRoutingDataSource )
2014-04-11 18:25 1595Spring动态配置多数据源,即在大型应用中对数据进行横向 ... -
(转)Spring Security 3.0 多页面登录配置
2012-10-19 17:01 5351网上很多文章是关于Spri ... -
Spring 中集成 JOTM 配置 JTA 事务
2011-12-27 19:52 1548Spring 中集成 JOTM 配置 JTA 事务: ... -
Spring 3.1整合EhCache
2011-08-29 16:42 1426Spring 3.1提供了对cache的 ... -
spring集成xfire教程
2011-04-02 15:20 1540配置web.xml <!-- SPRING ... -
关于Spring 声明式事务处理时,throws exception不回滚的问题
2010-09-29 23:31 8949前一段时间,项目代码评审,发现有TX不使用Spri ... -
iBatis SqlMap 的配置总结 收藏
2010-07-22 14:30 3711核心提示:SqlMap的配 ... -
ibatis oscache 使用中miss cache
2010-06-10 13:17 2841原始配置信息: <cacheModel id=&quo ... -
ibatis oscache配置以及【文件名、目录名或卷标语法不正确】错误解决
2010-05-26 13:13 3359sqlmap-config.xml 配置如下: < ... -
Hibernate+ehcache二级缓存技术
2010-02-01 20:57 8681、首先设置EhCache,建立配置文件ehcache.XML ... -
Hibernate的缓存讨论
2010-02-01 20:50 1073一般系统中有三种情况 ... -
OpenSessionInView Filter 但是发现不生效(转载)
2010-01-25 15:35 1739今天有一个朋友问了我一个问题,他使用的是Hibernate/S ... -
spring有三种启动方式,使用ContextLoaderServlet,ContextLoader
2010-01-19 17:25 1120spring有三种启动方式,使用ContextLoaderSe ... -
ApplicationContext和BeanFactory的关于单实例bean装载的区别
2010-01-19 17:04 1179Bean工厂延迟载入所有的Bean,直到getBean()方法 ... -
AOP术语(转载)
2010-01-06 21:04 983AOP术语(转载) 2009-07-16 22:12 ... -
spring Lookup方法注入(转)---cglib解决子类过多问题、重构
2010-01-06 17:33 5881“Lookup方法”可以使Spri ... -
Spring JTA应用之Atomikos配置
2010-01-05 23:17 3683Atomikos,是一个基于Java的开源事务管理器,提供了事 ... -
多对多关系中Set的查询
2010-01-05 22:27 1041多对多关系中Set的查询 一个老师教多个学生,一个学生 ... -
ibatis 使用 in子句
2009-09-28 18:44 1833因为in的性能问题,平常不怎么用。今天特殊需求,在ibatis ...
相关推荐
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 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-...
NULL 博文链接:https://microjava.iteye.com/blog/525796
描述一下Spring AOP? 在Spring AOP中关注点(concern)和横切关注点(cross-cutting concern)有什么不同? AOP有哪些可用的实现? Spring中有哪些不同的通知类型(advice types)? Spring AOP 代理是什么? 引介...
SpringAOP学习笔记以及四个可运行的Demo,涵盖经典代理模式、基于注解、基于xml配置这3方面的Demo
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
一、适合人群 1、具备一定Java编程基础,初级开发者 2、对springboot,mybatis,mysql有基本认识 3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 ...4、spring boot,mybatis,druid,spring aop的使用
spring aop的demo spring aop的demo
Spring框架的关键组件之一是面向方面编程(AOP)框架。 面向方面的编程需要将程序逻辑分解成不同的部分。 此教程将通过简单实用的方法来学习Spring框架提供的AOP/面向方面编程。
springaop依赖的jar包,spring版本2.5.6,如果需要,可以下载使用,欢迎各位评论指出不足
Spring Aop 学习笔记
Spring aop Spring aop
spring aop
springAOP详解
Spring AOP 几个不同使用方法的完整例子,使用Junit4c测试, 在我的博客上有不同配置组合的说明,可以参考
开发工具 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篇 - Spring AOP两种代理对象的拦截处理(csdn)————程序
Spring AOP简单demo 入门级的 advice