¡¡¡¡ÎÒÃÇʹÓÃÒ»¸ö¼òµ¥µÄÀý×ÓÀ´ÑÝʾһÏÂSpringÖеÄAOP£¬ÕâÊÇÒ»¸ölogµÄÀý×Ó£¬Êµ¼ÊÉÏlogÊÇÒ»¸ö¶ÔÓÚAOPÀ´ËµºÜ²»ºÃµÄÀý×Ó£¬ÕâÀïÎÒÃÇֻΪ˵Ã÷Spring AOPµÄʹÓᣠ¡¡¡¡ ¡¡¡¡1.Ê×ÏÈÎÒÃÇÀ´´´½¨Ò»¸ö×Ô¼ºµÄinterceptor ¡¡¡¡Õâ¸öÀà±ØÐë¼ÌÐøorg.aopalliance.intercept. MethodInterceptor½Ó¿Ú¡£SpringµÄAOP¿ò¼Ü¾ÍÊDzÎÕÕaopallianceÕâ¸ö±ê׼ʵÏֵģ¬ËùÒÔÎÒÃǵÄMyInterceptorÒª¼ÌÐøÕâ¸ö±ê×¼ÖеĽӿڡ£ ¡¡¡¡Õâ¸ö½Ó¿ÚÖ»ÓÐÒ»¸öÒªÇóʵÏֵķ½·¨£º ¡¡¡¡public Object invoke(MethodInvocation methodInvocation) throws Throwable; ¡¡¡¡ÏÂÃæÊÇÎÒÃǵÄMyIntercptor£º ¡¡¡¡ ¡¡¡¡public class MyInterceptor implements MethodInterceptor { ¡¡¡¡private final Log logger = LogFactory.getLog(getClass()); ¡¡¡¡ ¡¡¡¡public Object invoke(MethodInvocation methodInvocation) throws Throwable { ¡¡¡¡logger.info("Beginning method (1): " + ¡¡¡¡methodInvocation.getMethod().getDeclaringClass() + "." + ¡¡¡¡methodInvocation.getMethod().getName() + "()"); ¡¡¡¡long startTime = System.currentTimeMillis(); ¡¡¡¡try{ ¡¡¡¡Object result = methodInvocation.proceed(); ¡¡¡¡return result; ¡¡¡¡}finally{ ¡¡¡¡logger.info("Ending method (1): " + ¡¡¡¡methodInvocation.getMethod().getDeclaringClass() + "." + ¡¡¡¡methodInvocation.getMethod().getName() + "()"); ¡¡¡¡logger.info("Method invocation time (1): " + ¡¡¡¡(System.currentTimeMillis() - startTime) + " ms."); ¡¡¡¡} ¡¡¡¡} ¡¡¡¡} ¡¡¡¡ ¡¡¡¡¶ÔÓÚÉÏÃæµÄ´úÂëÐèҪ˵Ã÷µÄÊÇÏÂÃæÁ½ÐдúÂ룺 ¡¡¡¡Object result = methodInvocation.proceed(); ¡¡¡¡return result; ¡¡¡¡Õû¸ö³ÌÐòµÄÁ÷³ÌÊÇÕâÑùµÄ£º ¡¡¡¡1£¬ÏÈÊÇÖ´ÐÐÔÚObject result = methodInvocation.proceed();Ç°ÃæµÄ´úÂ룻 ¡¡¡¡2£¬½Ó×ÅÖ´ÐÐObject result = methodInvocation.proceed();£¬Ëü°ÑÖ´ÐпØÖÆÈ¨½»¸øÁËinterceptor stack(À¹½ØÆ÷Õ»)ÄÚµÄÏÂÒ»¸öinterceptor£¬¼ÙÈçûÓÐÁ˾ͽ»¸øÕæÕýµÄÒµÎñ·½·¨£» ¡¡¡¡3£¬È»ºóÖ´ÐÐreturn result;֮ǰµÄ´úÂ룻 ¡¡¡¡4£¬×îºóÖ´ÐÐreturn result;£¬Ëü°Ñ¿ØÖÆÈ¨½»»ØËüÖ®ÉϵÄinterceptor£¬¼ÙÈçûÓÐÁ˾ÍÍ˳öinterceptor stack¡£ ¡¡¡¡ ¡¡¡¡2.д³öÎÒÃǵÄÒµÎñ¶ÔÏó¼°Æä½Ó¿Ú ¡¡¡¡ÎªÁË·½±ãÎÒÃǵÄÒµÎñ½Ó¿ÚÖ»ÓÐÒ»¸öhello·½·¨£º ¡¡¡¡ ¡¡¡¡public interface BusinessInterface { ¡¡¡¡public void hello(); ¡¡¡¡} ¡¡¡¡ ¡¡¡¡ÒµÎñ¶ÔÏóµÄ´úÂëÈçÏ£º ¡¡¡¡ ¡¡¡¡public class BusinessInterfaceImpl implements BusinessInterface{ ¡¡¡¡public void hello() { ¡¡¡¡System.out.println("hello Spring AOP."); ¡¡¡¡} ¡¡¡¡} ¡¡¡¡ ¡¡¡¡3.½ÓÏÂÀ´£¬ÎÒÃÇÀ´¿´¿´ÈçºÎʹÓÃÎÒÃǵÄдµÄinterceptor ¡¡¡¡ÎÒÃǰÑÒµÎñ¶ÔÏó×÷ΪAOPµÄtarget£º ¡¡¡¡<bean id="businessTarget" class="com.rst.spring.testaop.BusinessInterfaceImpl"/> ¡¡¡¡½Ó×ÅÔÚbean¶¨ÒåÖÐÉùÃ÷interceptor£º ¡¡¡¡<bean id="myInterceptor" class="com.rst.spring.testaop.MyInterceptor"/> ¡¡¡¡×îºó£¬ÎÒÃÇÀ´ÉùÃ÷ÕæÕýµÄÒµÎñ¶ÔÏó£¬Í¨¹ýʹÓÃËüµÄ½Ó¿ÚÒÔ¼°SpringµÄProxyFactoryBean£º ¡¡¡¡ ¡¡¡¡<bean id="businessBean" ¡¡¡¡¡¡¡¡class="org.springframework.aop.framework.ProxyFactoryBean"> ¡¡¡¡<property name="proxyInterfaces"> ¡¡¡¡<value>com.rst.spring.testaop.BusinessInterface</value> ¡¡¡¡</property> ¡¡¡¡<property name="interceptorNames"> ¡¡¡¡<list> ¡¡¡¡<value>myInterceptor</value> ¡¡¡¡<value>businessTarget</value> ¡¡¡¡</list> ¡¡¡¡</property> ¡¡¡¡</bean> ¡¡¡¡ ¡¡¡¡ÕâÀïÐèҪ˵Ã÷Á½µã£º ¡¡¡¡proxyInterfaces£º¾ÍÊÇÎÒÃǵÄÒµÎñ¶ÔÏóµÄʵ¼Ê½Ó¿Ú£» ¡¡¡¡interceptorNames£º¶¨ÒåÁËËùÓÐinterceptorsµÄÖ´ÐÐ˳Ðò£¬ÆäÖÐÒµÎñ¶ÔÏóµÄtarget×÷ΪlistµÄ×îºóÒ»¸ö¡£¼Ç×ÅÒ»¶¨Òª°ÑÒµÎñ¶ÔÏóµÄtarget·Åµ½listÖУ¬·ñÔòÄãµÄÒµÎñ¶ÔÏó¾Í²»»á¹¤×÷¡£ ¡¡¡¡ ¡¡¡¡4.×îºó£¬Ð´ÎÒÃǵIJâÊÔÀà ¡¡¡¡ClassPathResource resource = ¡¡¡¡new ClassPathResource("com/rst/spring/testaop/aop_bean.XML"); ¡¡¡¡XmlBeanFactory beanFactory = new XmlBeanFactory(resource); ¡¡¡¡BusinessInterface businessBean = ¡¡¡¡(BusinessInterface) beanFactory.getBean("businessBean"); ¡¡¡¡businessBean.hello(); ¡¡¡¡ ¡¡¡¡Ò»ÇÐÕý³£¾Í¿ÉÒÔÔÚlogÉÏ¿´µ½ÏàÓ¦µÄÐÅÏ¢ÁË¡£ ¡¡¡¡ÒÔÏÂÊǸ½¼þÔ´´úÂëµÄÖ´ÐÐЧ¹û£º ¡¡¡¡2004-09-08 16:04:51,210 INFO - Beginning method (1): interface com.rst.spring.testaop.BusinessInterface.hello() ¡¡¡¡2004-09-08 16:04:51,210 INFO - Beginning method (2): interface com.rst.spring.testaop.BusinessInterface.hello() ¡¡¡¡hello Spring AOP. ¡¡¡¡2004-09-08 16:04:51,210 INFO - Ending method (2): interface com.rst.spring.testaop.BusinessInterface.hello() ¡¡¡¡2004-09-08 16:04:51,210 INFO - Ending method (1): interface com.rst.spring.testaop.BusinessInterface.hello() ¡¡¡¡2004-09-08 16:04:51,210 INFO - Method invocation time (1): 0 ms. ¡¡¡¡Ô´´úÂëÐèÒªspring.jar, aopallience.jar, commons-logging.jar¡£
|