In 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/bea NS/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 <! -- Scan annotation Bean --> 16 <context: component-scan base-package = "cn. Us. aspect"/> 17 <! -- Enable the aspect proxy so that spring knows @ aspect --> 18 <AOP: aspectj-autoproxy/>
Java file with no parameters
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) ") the following statements do not have parameters: 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}
With parameters,
Passing Parameters Using @ before fails, and using surround notification
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 =" execution (* CN. us. service. impl. userserviceimpl. adduser (..)) ") 19 public void pointcut () 20 {} 21 22 @ around (value =" pointcut () ") 23 public void aroundrecordlog (proceedingjoinpoint pjp) throws throwable24 {25 system. out. println ("around before"); 26 // obtain the 27 object [] objs = pjp. getargs (); 28 for (Object OBJ: objs) 29 {30 system. out. println (OBJ); 31} 32 // return value 33 object oo = pjp. proceed (); 34 35 system. out. println ("around after" + OO); 36} 37}