`
hahalzb
  • 浏览: 164846 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring AOP 概念

阅读更多
AOP即面向切面编程。

AOP中一些难以理解的概念:
引用

JoinPoint(连接点): 它定义在哪里(哪些点)加入你的逻辑功能,对于Spring AOP,Jointpoint指的就是Method.

引用

PointCut(切入点的集合):即一组Joinpoint,(通过正则表达式去匹配)就是说一个Advice可能在多个地方织入。
例如: @Pointcut("execution(* com.xyz.someapp.service.*.*(..))")
              public void businessService() {}
              返回值是任何类型,com.xyz.someapp.service下面的任何类的任何方法

引用

Aspect(切面): 实际是Advice和Pointcut的组合,但是Spring AOP 中的Advisor也是这样一个东西,但是Spring中为什么叫Advisor而不叫做Aspect。
(个人理解:两条不平行的线确定一个面,即Advice和Pointcut)
切面类里有好多方法,可以加到被代理对象上。

引用

Advice(通知):所谓通知是指拦截到jointpoint之后所要做的事情就是通知即特定的Jointpoint处运行的代码。
  对于Spring AOP 来讲,通知分为前置通知(Before advice)、后置通知(AfterreturningAdvice)、
  异常通知(ThrowAdvice)、最终通知(AfterThrowing)、环绕通知(AroundAdvice)。

引用
 
Target(目标对象):代理的目标对象即被通知的对象。

引用

Weave(织入): 指将aspects应用到target对象并导致proxy对象创建的过程称为织入
Introducton(引入):在不修改类代码的前提下,Introduction可以在运行期为类动态地添加一些方法或Field 。


在applicationContext.xml中使用<aop>,要想自动提示,需要手动去加入.
xmlns:aop="http://www.springframework.org/schema/aop"
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd



两种方式启用@Aspect支持:
1、在Spring配置文件中引入下列元素来启用Spring对@Aspect的支持。
<aop:aspectj-autoproxy /> 

2、在ApplicationContext.xml中添加如下定义来启用@Aspect支持。
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />


织入语法:
  execution(public * *(..))  --任何类的任何方法
  execution(* com.xyz.service..*.*(..))  --service包下面不管多少层的任何方法 ..代表任何层
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics