Spring AOP学习(二)--AspectJ

1.1 Spring的AspectJ的AOP(*)
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。
AspectJ是一个基于Java语言的AOP框架
Spring2.0以后新增了对AspectJ切点表达式支持
@AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
新版本Spring框架,建议使用AspectJ方式来开发AOP

AspectJ表达式:
* 语法:execution(表达式)
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

  • execution(“* cn.zq.spring3.demo1.dao.*(..)”) —只检索当前包
  • execution(“* cn.zq.spring3.demo1.dao..*(..)”) —检索包及当前包的子包.
  • execution(* cn.zq.dao.GenericDAO+.*(..)) —检索GenericDAO及子类

AspectJ增强:
@Before 前置通知,相当于BeforeAdvice
@AfterReturning 后置通知,相当于AfterReturningAdvice
@Around 环绕通知,相当于MethodInterceptor
@AfterThrowing抛出通知,相当于ThrowAdvice
@After 最终final通知,不管是否异常,该通知都会执行
@DeclareParents 引介通知,相当于IntroductionInterceptor (不要求掌握)

1.1.1 基于注解:
第一步:引入相应jar包.
* aspectj依赖aop环境.
* spring-aspects-3.2.0.RELEASE.jar
* com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

第二步:编写被增强的类:
* UserDao

第三步:使用AspectJ注解形式:
@Aspect
public class MyAspect {

@Before("execution(* cn.zq.spring3.demo1.UserDao.add(..))")
public void before(){
    System.out.println("前置增强....");
}

}

第四步:创建applicationContext.xml
* 引入aop的约束:
* < aop:aspectj-autoproxy /> — 自动生成代理:
* 底层就是AnnotationAwareAspectJAutoProxyCreator

<aop:aspectj-autoproxy />
<bean id="userDao" class="cn.zq.spring3.demo1.UserDao"></bean>
<bean id="myAspect" class="cn.zq.spring3.demo1.MyAspect"></bean>

AspectJ的通知类型:
@Before 前置通知,相当于BeforeAdvice
* 就在方法之前执行.没有办法阻止目标方法执行的.
@AfterReturning 后置通知,相当于AfterReturningAdvice
* 后置通知,获得方法返回值.
@Around 环绕通知,相当于MethodInterceptor
* 在可以方法之前和之后来执行的,而且可以阻止目标方法的执行.
@AfterThrowing抛出通知,相当于ThrowAdvice
@After 最终final通知,不管是否异常,该通知都会执行
@DeclareParents 引介通知,相当于IntroductionInterceptor (不要求掌握)

切点的定义:
@Pointcut(“execution(* cn.zq.spring3.demo1.UserDao.find(..))”)
private void myPointcut(){}

面试:
* Advisor和Aspect的区别?
* Advisor:Spring传统意义上的切面:支持一个切点和一个通知的组合.
* Aspect:可以支持多个切点和多个通知的组合.
1.1.2 基于XML:
第一步:编写被增强的类:
* ProductDao

第二步:定义切面

第三步:配置applicationContext.xmll

前置通知:
* 代码:
public void before(){
System.out.println(“前置通知…”);
}

  • 配置:
    < aop:config>

    < aop:pointcut expression=”execution(* cn.zq.spring3.demo2.ProductDao.add(..))” id=”mypointcut”/>
    < aop:aspect ref=”myAspectXML”>

    < aop:before method=”before” pointcut-ref=”mypointcut”/>
    < /aop:aspect>
    < /aop:config>

后置通知:
* 代码:
public void afterReturing(Object returnVal){
System.out.println(“后置通知…返回值:”+returnVal);
}

  • 配置:
    < aop:config>

    < aop:pointcut expression=”execution(* cn.zq.spring3.demo2.ProductDao.add(..))” id=”mypointcut”/>
    < aop:aspect ref=”myAspectXML”>

    < aop:after-returning method=”afterReturing” pointcut-ref=”mypointcut” returning=”returnVal”/>
    < / aop:aspect>
    < / aop:config>

环绕通知:
* 代码:
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println(“环绕前增强….”);
Object result = proceedingJoinPoint.proceed();
System.out.println(“环绕后增强….”);
return result;
}

  • 配置:
    < aop:config>

    < aop:pointcut expression=”execution(* cn.zq.spring3.demo2.ProductDao.add(..))” id=”mypointcut”/>
    < aop:aspect ref=”myAspectXML”>





    < aop:around method=”around” pointcut-ref=”mypointcut”/>
    < /aop:aspect>
    < /aop:config>

异常通知:
* 代码;
public void afterThrowing(Throwable e){
System.out.println(“异常通知…”+e.getMessage());
}

  • 配置;
    < aop:config>

    < aop:pointcut expression=”execution(* cn.zq.spring3.demo2.ProductDao.add(..))” id=”mypointcut”/>
    < aop:aspect ref=”myAspectXML”>

    < aop:after-throwing method=”afterThrowing” pointcut-ref=”mypointcut” throwing=”e”/>
    < /aop:aspect>
    < /aop:config>

最终通知:
* 代码:
public void after(){
System.out.println(“最终通知….”);
}

  • 配置:
    < aop:config>

    < aop:pointcut expression=”execution(* cn.zq.spring3.demo2.ProductDao.add(..))” id=”mypointcut”/>
    < aop:aspect ref=”myAspectXML”>

    < aop:after method=”after” pointcut-ref=”mypointcut”/>
    < /aop:aspect>
    < /aop:config>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑着巨人找巨人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值