Package Com.aop;import Org.apache.log4j.logger;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.proceedingjoinpoint;public class Myaspect {private static final Logger Logger=logger.getlogger ( Myaspect.class);//output to the specified file//private static final Logger logger=logger.getlogger ("Com.util.TimerLogger"); long a=0; public void Dobefore (Joinpoint jp) {object[] O=jp.getargs (), for (int i=0;i<o.length;i++) {System.err.println ("input parameter is "+o[i]);} A=system.currenttimemillis (); System.err.println ("Current Method execution Time is:" + jp.gettarget (). GetClass (). GetName () + "." + jp.getsignature (). GetName ());} public void Doafter (Joinpoint jp) {System.err.println ("Method end time is:" + jp.gettarget (). GetClass (). GetName () + "." + jp.getsignature (). GetName ()); System.err.println ("\r<br> Execution Time:" + (System.currenttimemillis ()-a) + "seconds");} public Object Doaround (Proceedingjoinpoint pjp) throws Throwable {long time = System.currenttimemillis (); object retVal = P Jp.proceed (); time = System.currenttimemillis ()-time;object[] O=pjp.getargs (); for(int i=0;i<o.length;i++) {Logger.info ("+ (i+1) +" Input parameter = "+o[i]);} Logger.info ("Current method is" + Pjp.gettarget (). GetClass (). GetName () + "." + pjp.getsignature (). GetName () + "Execution Time" +time+ "MS"); return retVal;} public void dothrowing (Joinpoint JP, Throwable ex) {System.out.println ("method" + jp.gettarget (). GetClass (). GetName () + "." + jp.getsignature (). GetName () + "throw exception"); System.out.println (Ex.getmessage ());}}
Spring configuration file
<aop:config><aop:aspect id= "Concurrentoperationretry" ref= "Myaspect" ><aop:pointcut id= " Idempotentoperation " expression=" Execution (* com.service.imp.*.* (..)) " /> <!--<aop:before pointcut-ref= "idempotentoperation" method= "Dobefore"/><aop:after pointcut-ref= "Idempotentoperation" method= "Doafter"/>--><aop:around pointcut-ref= "IdempotentOperation" Method= "Doaround"/></aop:aspect></aop:config>
Test method
Package Function;import Java.io.ioexception;import Org.junit.test;import com.model.user;import Com.service.userservice;import Com.util.beanfactoryutil;public class Myaspecttest {@Testpublic void Aoptest () throws Ioexception{userservice userservice= (UserService) beanfactoryutil.getinstance (). Getbean ("UserService"); User User=userservice.getuserbyuid (3); System.err.println (User.getname ());}}
Console output
[14:32:22,192 INFO] [Main] Imp. Getuserbyuid execution end in Userserviceimpl-userserviceimpl
[14:32:22,193 INFO] [Main] AOP. Myaspect-1th Input parameter = 3
[14:32:22,194 INFO] [Main] AOP. Myaspect-The current method is Com.service.imp.UserServiceImpl.getUserByUid execution time is 459ms
AOP is an abbreviation for aspect oriented programming, meaning:Plane-oriented programming, throughPre-compilationmethod and run-time dynamic agent to realize the unified maintenance of the program functions of a technology. AOP isOopis a hot spot in software development, but alsoSpringan important part of the framework isFunction-Type programmingof a derivative model. AOP enables you to isolate parts of your business logic so that the parts of your business logicCoupling degreereduce, improve the reusability of the program, and improve the efficiency of development.
Configuration file Description
1 Write the Slice class.
2 define Pointcuts. Pointcut Specifying pointcuts
3 define notifications. (Enhanced processing of target objects)
Common notification Types:
Intercept surround notification around
Pre-notification before
Exception Notification After-throwingmethod
Post notification after
Record Com.service.imp.*.* (..)) Execution time of all methods
The first * records all the classes under the IMP package, and the second * is all methods under all classes of records (.. ) indicates that all input parameters are matched.
1 The Around () method of the pre-execution slice of the target method records the current system time as a,
2 Execute Target Object Method Object RetVal = Pjp.proceed ();
3 The Around () method of the execution slice after the target method executes records the current system time to B
Method execution Time =b-a;
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Log execution time for each method AOP