Spring的AOP的注解的通知类型,切入点的注解

Spring的注解的AOP的通知类型

  • @Before:前置通知

  • @AfterReturning:后置通知

  • @Around:环绕通知

  • @AfterThrowing:异常抛出通知

  • @After:最终通知

  • @Pointcut:切入点的注解

    1 /** 2 * 切面类:注解的切面类 3 / 4 @Aspect 5 public class MyAspectAnno { 6 //前置通知 7 @Before(value="execution( com.itheima.spring.demo1.OrderDao.save(..) )") 8 public void before(){ 9 System.out.println("前置通知======"); 10 } 11 //后置通知 12 @AfterReturning(value="execution(* com.itheima.spring.demo1.OrderDao.delete(..))", returning="result") 13 public void afterReturning(Object result){ 14 System.out.println("后置通知====="+result); 15 } 16 @Around(value="execution(* com.itheima.spring.demo1.OrderDao.update(..))") 17 public Object around(ProceedingJoinPoint joinPoint) throws Throwable{ 18 System.out.println("环绕前增强====="); 19 Object obj = joinPoint.proceed(); 20 System.out.println("环绕后增强====="); 21 return obj; 22 } 23 //异常抛出通知 24 @AfterThrowing(value="execution(* com.itheima.spring.demo1.OrderDao.find(..))" , throwing="e") 25 public void find(Throwable e ){ 26 System.out.println("异常抛出通知======"+e.getMessage()); 27 } 28 //最终通知: 29 @After(value="execution(* com.itheima.spring.demo1.OrderDao.find(..))") 30 public void after( ){ 31 System.out.println("最终通知======"); 32 } 33 }

 切入点的注解:

配置@Pointcut注解,使用类名.方法

1/** 2 * 切面类:注解的切面类 3 */ 4@Aspect 5public class MyAspectAnno { 6 //切入点的注解 7 @Pointcut(value="execution(* com.itheima.spring.demo1.OrderDao.find(..))") 8 private void pointcut1(){} 9 10 @Pointcut(value="execution(* com.itheima.spring.demo1.OrderDao.save(..))") 11 private void pointcut2(){} 12 13 @Pointcut(value="execution(* com.itheima.spring.demo1.OrderDao.update(..))") 14 private void pointcut3(){} 15 16 @Pointcut(value="execution(* com.itheima.spring.demo1.OrderDao.delete(..))") 17 private void pointcut4(){} 18 19 //前置通知 20 @Before(value="MyAspectAnno.pointcut2()") 21 public void before(){ 22 System.out.println("前置通知======"); 23 } 24 //后置通知 25 @AfterReturning(value="MyAspectAnno.pointcut4()", returning="result") 26 public void afterReturning(Object result){ 27 System.out.println("后置通知====="+result); 28 } //环绕通知 29 @Around(value="MyAspectAnno.pointcut3()") 30 public Object around(ProceedingJoinPoint joinPoint) throws Throwable{ 31 System.out.println("环绕前增强====="); 32 Object obj = joinPoint.proceed(); 33 System.out.println("环绕后增强====="); 34 return obj; 35 } 36 //异常抛出通知 37 @AfterThrowing(value="MyAspectAnno.pointcut1()" , throwing="e") 38 public void find(Throwable e ){ 39 System.out.println("异常抛出通知======"+e.getMessage()); 40 } 41 42 // 最终通知: 43 @After(value="MyAspectAnno.pointcut1()") 44 public void after( ){ 45 System.out.println("最终通知======"); 46 } 47 48 49 50}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java中AOP的环绕通知

pom.xml<dependencies<dependency<groupIdorg.springframework</groupId<artifactIdspringcontext</artifactId

javaWeb

<fontsize5style"lineheight:32px;letterspacing:1px;"<fontcolorred一</font:拦截器:是在面向切面编程的就是在你的service或者一个方法,前调用一个方法,或者在方法后调用一个方法比如动态代理就是拦截器的简单实现,springmvc的aop中的前置通知和后置通知。

SpringAOP

Aspect切面:一个关注点的模块化,这个关注点可能会横切多个对象Joinpoint连接点:程序执行过程中的某个特定的点Advice通知:在切面的某个连接点上执行的动作Pointcut切入点:匹配连接点的断言,在AOP的通知和一个切入点表达式关联Introduction引入:在不修改类代码的前提下,为类添加新的方法