spring.xml中java
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文件中 無參數的spring
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寫法dom
有參數 傳遞的寫法 @Before和 @After 的形參都要寫上 參數spa
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()方法獲取參數。而且輸出code
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 }