在基于Spring AOP编程的过程当中,基于AspectJ框架标准,spring中定义了五种类型的通知,它们分别是:java
将上面的全部通知类型写入同一个切面中,它的执行顺序为:c++
package com.cy.pj.common.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect @Component public class SysTimeAspect { /** * 切入点 */ @Pointcut("bean(sysMenuServiceImpl)") public void doTime(){} @Before("doTime()") public void doBefore(JoinPoint jp){ System.out.println("time doBefore()"); } @After("doTime()") public void doAfter(){//相似于finally{}代码块 System.out.println("time doAfter()"); } /**核心业务正常结束时执行 * 说明:假若有after,先执行after,再执行returning*/ @AfterReturning("doTime()") public void doAfterReturning(){ System.out.println("time doAfterReturning"); } /**核心业务出现异常时执行 * 说明:假若有after,先执行after,再执行Throwing*/ @AfterThrowing("doTime()") public void doAfterThrowing(){ System.out.println("time doAfterThrowing"); } @Around("doTime()") public Object doAround(ProceedingJoinPoint jp) throws Throwable{ System.out.println("doAround.before"); try { Object obj=jp.proceed(); return obj; }catch(Throwable e) { System.out.println("doAround.error-->"+e.getMessage()); throw e; }finally { System.out.println("doAround.after"); } } }
欢迎关注个人公众号【穿着条纹睡衣的男孩】,由于才注册,因此须要你们的大力支持啊!!! 关注后可领取java和c++的学习资源视频,做为礼物!!!
spring