首页 > 代码库 > aop注解 自定义切面的注解写法

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

spring.xml中

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4     xmlns:context="http://www.springframework.org/schema/context" 5     xmlns:aop="http://www.springframework.org/schema/aop" 6     xmlns:tx="http://www.springframework.org/schema/tx" 7     xsi:schemaLocation="http://www.springframework.org/schema/beans  8                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9                          http://www.springframework.org/schema/context10                          http://www.springframework.org/schema/context/spring-context-3.0.xsd11                          http://www.springframework.org/schema/tx12                          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd13                          http://www.springframework.org/schema/aop 14                          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">15 <!--      扫描注解bean -->16      <context:component-scan base-package="cn.us.aspect"/>17 <!--      开启切面代理 使得spring认识 @Aspect -->18      <aop:aspectj-autoproxy/>

 

 java文件中   无参数的

 1 package cn.us.aspect; 2  3  4 import org.aspectj.lang.annotation.AfterReturning; 5 import org.aspectj.lang.annotation.Aspect; 6 import org.aspectj.lang.annotation.Before; 7 import org.aspectj.lang.annotation.Pointcut; 8 import org.springframework.stereotype.Component; 9 10 import cn.us.domain.User;11 12 @Component("logAspect")13 @Aspect14 public class LogAspect {15     // @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.addUser(cn.us.domain.User)) and args(user)")   以下是无参数的写法16      @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(..))")17      public void pointcut()18      {}19     // @Before("pointcut(user)")20       @Before("pointcut()")21     // public void startRecordLog(User user)22        public void startRecordLog()23      {24          System.out.println("Before log  has started");25          System.out.println();26      }27 28      @AfterReturning(value="http://www.mamicode.com/pointcut()",returning="val")29      public void endRecordLog(Object val)30      {31          System.out.println("@AfterReturning log  has end");32          System.out.println(val);33      }34 }

 

 有参数的,

利用@before传递参数不成功,利用环绕通知

 1 package cn.us.aspect; 2  3  4 import org.aspectj.lang.ProceedingJoinPoint; 5 import org.aspectj.lang.annotation.AfterReturning; 6 import org.aspectj.lang.annotation.Around; 7 import org.aspectj.lang.annotation.Aspect; 8 import org.aspectj.lang.annotation.Before; 9 import org.aspectj.lang.annotation.Pointcut;10 import org.springframework.stereotype.Component;11 12 import cn.us.domain.User;13 14 @Component("logAspect")15 @Aspect16 public class LogAspect {17     // @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(cn.us.domain.User)) and args(user)")18      @Pointcut(value="http://www.mamicode.com/execution(* cn.us.service.impl.UserServiceImpl.addUser(..))")19      public void pointcut()20      {}21 22      @Around(value = "http://www.mamicode.com/pointcut()")23      public void aroundRecordLog(ProceedingJoinPoint pjp) throws Throwable24      {25          System.out.println("around before");26          //获取参数27          Object [] objs=pjp.getArgs();28          for(Object obj :objs)29          {30              System.out.println(obj);31          }32          //返回值33          Object oo=pjp.proceed();34         35          System.out.println("around after"  +oo);36      }37 }