ÈÕÖ¾¼Í¼Óë¼à¿Ø
ÔÚ´ó?´ó¶¼ÏîÄ¿ÖУ¬ÈÕÖ¾¼Í¼ºÍ¼à¿ØÊDz»¿É»òȱµÄ¹¦Ð§¡£Í¨¹ýÐÔ°Í¿ËAOP£¬ÎÒÃÇ¿ÉÒÔÔÚ²»ÐÞ¸ÄÓªÒµ´úÂëµÄÇéÐÎÏ£¬¶ÔÒªÁìŲÓþÙÐÐÈÕÖ¾¼Í¼¡£
@AspectpublicclassLoggingAspect{@Around("execution(*com.example.service.*.*(..))")publicObjectlogAround(ProceedingJoinPointjoinPoint)throwsThrowable{longstart=System.currentTimeMillis();try{System.out.println("Executingmethod:"+joinPoint.getSignature().getName());returnjoinPoint.proceed();}finally{longduration=System.currentTimeMillis()-start;System.out.println("Methodexecutiontime:"+duration+"ms");}}}
ÐÔÄÜÓÅ»¯
ÐÔÄÜÓÅ»¯ÊÇÌáÉýÊÂÇéЧÂʵÄÖ÷Òª·½Ã档ͨ¹ýÐÔ°Í¿ËAOP£¬ÎÒÃÇ¿ÉÒÔÔÚ²»ÐÞ¸ÄÓªÒµ´úÂëµÄÇéÐÎÏ£¬¶ÔÒªÁìŲÓþÙÐÐÐÔÄÜ¼à¿ØºÍÓÅ»¯¡£
@AspectpublicclassPerformanceAspect{@Around("execution(*com.example.service.*.*(..))")publicObjectmonitorPerformance(ProceedingJoinPointjoinPoint)throwsThrowable{longstart=System.currentTimeMillis();try{System.out.println("Executingmethod:"+joinPoint.getSignature().getName());returnjoinPoint.proceed();}finally{longduration=System.currentTimeMillis()-start;System.out.println("Methodexecutiontime:"+duration+"ms");}}}
ʲôÊÇÐÔ°Í?¿ËAOP£¿
ÐÔ°Í¿ËAOPÊÇÒ»ÖÖ±à³Ì·¶Ê½£¬Ö¼ÔÚ½«ºáÇйØ×¢µã£¨ÈçÈÕÖ¾¼Í¼¡¢ÊÂÎñ¹ÜÀí¡¢Çå¾²¿ØÖƵȣ©´ÓÓªÒµÂß¼ÖгéÀë³öÀ´£¬Í¨¹ý¡°ÇÐÃæ¡±£¨Aspect£©ºÍ¡°Í¨Öª¡±£¨Advice£©À´´¦Öóͷ£¡£Ïà±È¹Å°åµÄ?±à³Ì·½·¨£¬AOPÄܹ»Ô½·¢ÇåÎúµØÊèɢϵͳµÄ¸÷¸ö¹¦Ð§Ä£¿é£¬Ìá¸ß´úÂëµÄ¿Éά»¤ÐԺͿɶÁÐÔ¡£
ͨ¹ýÉÏÊöÏÈÈÝ£¬ÎÒÃÇÏàʶÁËÐÔ°Í¿ËAOPµÄ»ù±¾¿´·¨¡¢½¹µãÓÅÊÆÒÔ¼°ÔõÑùÔÚÏÖʵÊÂÇéÖÐÓ¦ÓÃËüÀ´ÌáÉýÊÂÇéЧÂÊ¡£ÎÞÂÛÊÇÈÕÖ¾¼Í¼¡¢ÊÂÎñ¹ÜÀí£¬ÕվɯäËûºáÇйØ×¢µã£¬AOP¶¼ÄÜ×ÊÖúÎÒÃǸü¸ßЧµØ¹ÜÀíºÍÓÅ»¯´úÂë¡£ÔÚÖ°³¡ÖУ¬ÕÆÎÕAOPÊÖÒÕ²»µ«ÄÜÌá¸ßÎÒÃǵıà³Ìˮƽ£¬¸üÄÜÏÔÖøÌáÉýÎÒÃǵÄÊÂÇéЧÂÊ£¬ÎªÐ¡ÎÒ˽¼ÒºÍÍŶӴøÀ´¸ü´óµÄ¼ÛÖµ¡£
ÔÚǰһ²¿·ÖÖУ¬ÎÒÃÇÏÈÈÝÁËʲôÊÇÐÔ°Í¿ËAOPÒÔ¼°ÔõÑùʹÓÃËüÀ´ÌáÉýÊÂÇéЧÂÊ¡£±¾²¿·Ö½«½øÒ»²½ÉîÈë̽ÌÖÐÔ°Í¿ËAOPµÄ¸ß¼¶Ó¦Óü¼ÇÉ£¬²¢?Ìṩ¸ü¶àÏÖʵ°¸Àý£¬ÒÔ×ÊÖúÄã¸üÖÜÈ«µØÕÆÎÕÕâÒ»ÊÖÒÕ£¬´Ó?¶øÔÚÏÖʵÊÂÇéÖÐʩչ×î´óµÄЧÄÜ¡£
ÐÔÄÜ¼à¿Ø
ͨ¹ýAOP£¬ÎÒÃÇ¿ÉÒÔÔÚ²»ÐÞ¸ÄÏêϸӪҵ´úÂëµÄ?ÇéÐÎÏ£¬ÊµÏÖ¶ÔÒªÁìµÄÐÔÄÜ¼à¿Ø¡£
@Aspect@ComponentpublicclassPerformanceAspect{@Around("execution(*com.example.service.*.*(.*))")publicObjectmonitorPerformance(ProceedingJoinPointjoinPoint)throwsThrowable{longstart=System.currentTimeMillis();Objectresult=joinPoint.proceed();longfinish=System.currentTimeMillis();System.out.println("ÐÔÄÜ¼à¿Ø£ºÒªÁì"+joinPoint.getSignature().getName()+"ºÄʱ:"+(finish-start)+"ms");returnresult;}}
У¶Ô£ºÕÔÉÙ¿µ(f3J1ePQDlzHhwh44q38w4Ima2E3XrDq)



