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/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 13 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 @Aspect 14 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="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和@After写法
有参数 传递的写法 @Before和 @After 的形参都要写上 参数
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 @Aspect 16 public class LogAspect { 17 // @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(cn.us.domain.User)) and args(user)") 18 @Pointcut(value="execution(* cn.us.service.impl.UserServiceImpl.addUser(cn.us.domain.User)) && args(user) ") 19 public void pointcut(User user) 20 {} 21 // @Before(value="pointcut(user)") 22 // public void startRecordLog(cn.us.domain.User user) 23 // @Before("pointcut()") 24 // public void startRecordLog() 25 @Before(value="pointcut(user)") 26 public void startRecordLog(User user) 27 { 28 System.out.println("Before log has started"); 29 System.out.println(user); 30 } 31 32 @AfterReturning(value="pointcut(user)",returning="val") 33 public void endRecordLog(User user,Object val) 34 { 35 User u=(User) val; //这里要强制类型转换,否则输出的位null ,没有强转调用object的,所以就为null了 36 System.out.println("@AfterReturning log has end"); 37 System.out.println(u); 38 } 39 40 @Around(value = "pointcut(user)") 41 public void aroundRecordLog(ProceedingJoinPoint pjp,User user) throws Throwable 42 { 43 System.out.println("around before"); 44 //获取参数 45 Object [] objs=pjp.getArgs(); 46 for(Object obj :objs) 47 { 48 System.out.println(obj); 49 } 50 //返回值 51 Object oo=pjp.proceed(); 52 53 System.out.println("around after" +oo); 54 } 55 }
无参数的写法,利用环绕通知
可以再环绕通知里 利用getArgs()方法获取参数。并且输出
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 @Aspect 16 public class LogAspect { 17 // @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(cn.us.domain.User)) and args(user)") 18 @Pointcut(value="execution(* cn.us.service.impl.UserServiceImpl.addUser(..)) ") 19 public void pointcut() 20 {} 21 22 @Around(value = "pointcut()") 23 public void aroundRecordLog(ProceedingJoinPoint pjp) throws Throwable 24 { 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 }