½¹µã¿´·¨
ÇÐÃæ£¨Aspect£©£º°üÀ¨Á˺áÇйØ×¢µãµÄ´úÂë¡£ËüÊÇAOPµÄ»ù±¾µ¥Î»¡£Á¬½Óµã£¨JoinPoint£©£º³ÌÐòÖ´ÐÐÀú³ÌÖпÉÇÐÈëµÄµã£¬ÈçÒªÁìŲÓá¢Òì³£Å׳öµÈ¡£ÇÐÈëµã£¨Pointcut£©£º½ç˵ÔÚÄÄЩÁ¬½ÓµãÓ¦ÓÃÇÐÃæµÄ¹æÔò¡£Í¨Öª£¨Advice£©£ºÊµ¼ÊÔÚÁ¬½ÓµãÉÏÖ´ÐеĴúÂ룬¿ÉÒÔÊÇǰÖÃ֪ͨ¡¢ºóÖÃ֪ͨ¡¢Ò쳣֪ͨµÈ¡£
ÊÂÎñÖÎÀí
ÊÂÎñÖÎÀíÊÇÊý¾Ý¿â²Ù×÷Öг£¼ûµÄÐèÇó¡£Í¨¹ýÐÔ°Íͨ¹ýÐÔ°Í¿ËAOP£¬ÎÒÃÇ¿ÉÒÔÔÚ²»ÐÞ¸ÄÒµÎñ´úÂëµÄÇé¿öÏ£¬È·±£ÊÂÎñµÄÕýÈ·ÐÔºÍÒ»ÖÂÐÔ¡£
@AspectpublicclassTransactionAspect{@Around("execution(*com.example.service.*.*(..))")publicObjectmanageTransaction(ProceedingJoinPointjoinPoint)throwsThrowable{TransactionStatusstatus=TransactionAspect.transactionManager.getTransaction(newDefaultTransactionDefinition());try{Objectresult=joinPoint.proceed();transactionManager.commit(status);returnresult;}catch(Exceptione){transactionManager.rollback(status);throwe;}}}
ÐÔ°Í¿ËAOPµÄ½¹µãÓÅÊÆ
´úÂ븴ÓãºÍ¨¹ý½«ºáÇйØ×¢µãÌáÈ¡³öÀ´£¬¿ÉÒÔÔÚ¶à¸öµØ·½¸´ÓÃÕâЩ¹¦Ð§£¬ÖÆÖ¹´úÂëÖØ¸´¡£Ìá¸ß¿Éά»¤ÐÔ£º½«ºáÇйØ×¢µãµ¥¶ÀÌáÈ¡³öÀ´£¬Ê¹µÃ½¹µãÒµÎñÂß¼Ô½·¢ÇåÎú£¬±ã?ÓÚά»¤ºÍÐ޸ġ£ÌáÉý¿ª·¢Ð§ÂÊ£ºÍ¨¹ýAOP£¬¿ª·¢ÈËÔ±¿ÉÒÔרעÓÚ½¹µãÒµÎñÂß¼£¬¶ø²»±Ø¹ý¶à¹Ø×¢ºáÇйØ×¢µã£¬´Ó¶øÌá¸ßÕûÌ忪·¢Ð§ÂÊ¡£
ÈÕÖ¾¼Í¼
ÈÕÖ¾¼Í¼ÊÇAOPÓ¦ÓÃ×î³£¼ûµÄ³¡¾°Ö®Ò»¡£Í¨¹ýAOP£¬ÎÒÃÇ¿ÉÒÔÔÚ²»ÐÞ¸ÄÒµÎñ´úÂëµÄ?Çé¿öÏ£¬¶¯Ì¬µØ¼Í¼ҪÁìÖ´ÐеÄÐÅÏ¢¡£
@Aspect@ComponentpublicclassLoggingAspect{@Before("execution(*com.example.service.*.*(.*))")publicvoidlogBefore(JoinPointjoinPoint){System.out.println("Ö´ÐÐǰÈÕÖ¾£º"+joinPoint.getSignature().getName());}@AfterReturning(pointcut="execution(*com.example.service.*.*(.*))",returning="result")publicvoidlogAfterReturning(JoinPointjoinPoint,Objectresult){System.out.println("Ö´ÐкóÈÕÖ¾£º"+joinPoint.getSignature().getName()+"·µ»ØÖµ:"+result);}@AfterThrowing(pointcut="execution(*com.example.service.*.*(.*))",throwing="error")publicvoidlogAfterThrowing(JoinPointjoinPoint,Throwableerror){System.out.println("Òì³£ÈÕÖ¾£º"+joinPoint.getSignature().getName()+"Òì³£ÐÅÏ¢:"+error.getMessage());}}
ÊÂÎñÖÎÀí
ÔÚÊý¾Ý²Ù×÷ÖУ¬ÊÂÎñÖÎÀíÊǺÜÊÇÖØÒªµÄ¡£Í¨¹ýAOP£¬ÎÒÃÇ¿ÉÒÔÔÚ²»ÐÞ¸ÄÒµÎñ´úÂëµÄ?Çé¿öÏ£¬¶¯Ì¬µØ?ÖÎÀíÊÂÎñ¡£
@Aspect@ComponentpublicclassTransactionAspect{@Around("execution(*com.example.repository.*.*(.*))")publicObjectmanageTransaction(ProceedingJoinPointjoinPoint)throwsThrowable{TransactionStatusstatus=TransactionAspectSupport.createTransactionStatus("tx");try{TransactionAspectSupport.startTransaction(status);Objectresult=joinPoint.proceed();TransactionAspectSupport.commitTransaction(status);returnresult;}catch(Exceptione){TransactionAspectSupport.rollbackTransaction(status);throwe;}}}
ÔÚÐÔ°Í¿ËAOPÖУ¬Ö÷ÒªÓÐÒÔϼ¸¸ö¿´·¨£º
ÇÐÃæ£¨Aspect£©£ºÒ»¸ö°üÀ¨ºáÇйØ×¢µãµÄÄ£¿é£¬Í¨³£°üÀ¨ÇÐÈëµã¡¢Í¨ÖªºÍµãcutµÈ?¡£Á¬½Óµã£¨JoinPoint£©£º³ÌÐòÖ´ÐÐÀú³ÌÖеÄÌØ¶¨µã£¬ÀýÈçÒªÁìŲÓÃǰºó¡£ÇÐÈëµã£¨Pointcut£©£º½ç˵ÔÚÄÄЩÁ¬½ÓµãÉÏÓ¦ÓúáÇйØ×¢µãµÄ¹æÔò¡£Í¨Öª£¨Advice£©£ºÔÚÁ¬½ÓµãÉÏÖ´ÐеĴúÂ룬¿ÉÒÔÊÇǰÖÃ֪ͨ¡¢ºóÖÃ֪ͨ¡¢Î§ÈÆÍ¨ÖªµÈ¡£
ͨ¹ýÉÏÊö½éÉÜ£¬ÎÒÃÇÁ˽âÁËÐÔ°Í¿ËAOPµÄ»ù±¾¿´·¨¡¢½¹µãÓÅÊÆÒÔ¼°ÈçºÎÔÚʵ¼ÊÊÂÇéÖÐÓ¦ÓÃËüÀ´ÌáÉýÊÂÇéЧÂÊ¡£ÎÞÂÛÊÇÈÕÖ¾¼Í¼¡¢ÊÂÎñÖÎÀí£¬ÕվɯäËûºáÇйØ×¢µã£¬AOP¶¼ÄÜ×ÊÖúÎÒÃǸü¸ßЧµØÖÎÀíºÍÓÅ»¯´úÂë¡£ÔÚÖ°³¡ÖУ¬ÕÆÎÕAOP¼¼Êõ²»µ«ÄÜÌá¸ßÎÒÃǵıà³Ìˮƽ£¬¸üÄÜÏÔÖøÌáÉýÎÒÃǵÄÊÂÇéЧÂÊ£¬Îª¸öÈ˺ÍÍŶӴøÀ´¸ü´óµÄ¼ÛÖµ¡£
ÔÚǰһ²¿·ÖÖУ¬ÎÒÃǽéÉÜÁËʲôÊÇÐÔ°Í¿ËAOPÒÔ¼°ÈçºÎʹÓÃËüÀ´ÌáÉýÊÂÇéЧÂÊ¡£±¾²¿·Ö½«½øÒ»²½ÉîÈë̽ÌÖÐÔ°Í¿ËAOPµÄ¸ß¼¶Ó¦Óü¼ÇÉ£¬²¢Ìṩ¸ü¶àʵ¼Ê°¸Àý£¬ÒÔ×ÊÖúÄã¸üÈ«ÃæµØÕÆÎÕÕâÒ»¼¼Êõ£¬´Ó¶øÔÚʵ¼ÊÊÂÇéÖз¢»Ó×î´óµÄЧÄÜ¡£
ºóÖ÷µ»ØÍ¨Öª£¨AfterReturning£©
ÔÚÄ¿±êÒªÁìÀÖ³ÉÖ´Ðк󣬵«ÔÚÎÒÃǶԽá¹û½øÐÐÈÎÄDZßÖÃ֮ǰִÐС£
@Aspect@ComponentpublicclassPostExecutionLoggingAspect{@AfterReturning(pointcut="execution(*com.example.service.*.*(.*))",returning="result")publicvoidlogAfterReturning(JoinPointjoinPoint,Objectresult){System.out.println("ºóÖ÷µ»ØÍ¨Öª£ºÒªÁì"+joinPoint.getSignature().getName()+"·µ»ØÖµ:"+result);}}
У¶Ô£ºÁÖºÍÁ¢(1C0m4pJyqZtPma0S7t9ZFfz4hTykKag)


