aop注解 自定义切面的注解写法分析

哈哈 阅读:302 2019-12-19 19:02:30 评论:0

spring.xml中

 <?xml version="1.0" encoding="UTF-8"?> 
 <beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans  
                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                          http://www.springframework.org/schema/context 
                          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
                          http://www.springframework.org/schema/tx 
                          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
                          http://www.springframework.org/schema/aop  
                          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
 <!--      扫描注解bean --> 
      <context:component-scan base-package="cn.us.aspect"/> 
 <!--      开启切面代理 使得spring认识 @Aspect --> 
      <aop:aspectj-autoproxy/>

 java文件中   无参数的

 package cn.us.aspect; 
  
  
 import org.aspectj.lang.annotation.AfterReturning; 
 import org.aspectj.lang.annotation.Aspect; 
 import org.aspectj.lang.annotation.Before; 
 import org.aspectj.lang.annotation.Pointcut; 
 import org.springframework.stereotype.Component; 
  
 import cn.us.domain.User; 
  
 @Component("logAspect") 
 @Aspect 
 public class LogAspect { 
     // @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.addUser(cn.us.domain.User)) and args(user)")   以下是无参数的写法 
      @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(..))") 
      public void pointcut() 
      {} 
     // @Before("pointcut(user)") 
       @Before("pointcut()") 
     // public void startRecordLog(User user) 
        public void startRecordLog() 
      { 
          System.out.println("Before log  has started"); 
          System.out.println(); 
      } 
  
      @AfterReturning(value="pointcut()",returning="val") 
      public void endRecordLog(Object val) 
      { 
          System.out.println("@AfterReturning log  has end"); 
          System.out.println(val); 
      } 
 }

 

有参数的 利用 @Before和@After写法

有参数 传递的写法 @Before和 @After 的形参都要写上 参数

 package cn.us.aspect; 
  
  
 import org.aspectj.lang.ProceedingJoinPoint; 
 import org.aspectj.lang.annotation.AfterReturning; 
 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; 
  
 import cn.us.domain.User; 
  
 @Component("logAspect") 
 @Aspect 
 public class LogAspect { 
     // @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(cn.us.domain.User)) and args(user)") 
     @Pointcut(value="execution(* cn.us.service.impl.UserServiceImpl.addUser(cn.us.domain.User)) && args(user) ") 
      public void pointcut(User user) 
      {} 
     //     @Before(value="pointcut(user)") 
     //     public void startRecordLog(cn.us.domain.User user) 
      // @Before("pointcut()") 
      // public void startRecordLog() 
      @Before(value="pointcut(user)") 
      public void startRecordLog(User user) 
      { 
          System.out.println("Before log  has started"); 
          System.out.println(user); 
      } 
  
      @AfterReturning(value="pointcut(user)",returning="val") 
      public void endRecordLog(User user,Object val) 
      { 
          User u=(User) val;                          //这里要强制类型转换,否则输出的位null ,没有强转调用object的,所以就为null了 
          System.out.println("@AfterReturning log  has end"); 
          System.out.println(u); 
      } 
       
      @Around(value = "pointcut(user)") 
      public void aroundRecordLog(ProceedingJoinPoint pjp,User user) throws Throwable 
      { 
          System.out.println("around before"); 
          //获取参数 
          Object [] objs=pjp.getArgs(); 
          for(Object obj :objs) 
          { 
              System.out.println(obj); 
          } 
          //返回值 
          Object oo=pjp.proceed(); 
          
          System.out.println("around after"  +oo); 
      } 
 }

 

 无参数的写法,利用环绕通知

可以再环绕通知里 利用getArgs()方法获取参数。并且输出

 package cn.us.aspect; 
  
  
 import org.aspectj.lang.ProceedingJoinPoint; 
 import org.aspectj.lang.annotation.AfterReturning; 
 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; 
  
 import cn.us.domain.User; 
  
 @Component("logAspect") 
 @Aspect 
 public class LogAspect { 
     // @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(cn.us.domain.User)) and args(user)") 
      @Pointcut(value="execution(* cn.us.service.impl.UserServiceImpl.addUser(..)) ") 
      public void pointcut() 
      {} 
  
      @Around(value = "pointcut()") 
      public void aroundRecordLog(ProceedingJoinPoint pjp) throws Throwable 
      { 
          System.out.println("around before"); 
          //获取参数 
          Object [] objs=pjp.getArgs(); 
          for(Object obj :objs) 
          { 
              System.out.println(obj); 
          } 
          //返回值 
          Object oo=pjp.proceed(); 
          
          System.out.println("around after"  +oo); 
      } 
 }

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

扫一扫关注我们,了解最新精彩内容