PrepareInterceptor À¹½ØÆ÷¼ò½é
¸ÃÀ¹½ØÆ÷´¦ÓÚdefaultStackµÚÎåµÄλÖ㬸ÃÀ¹½ØÆ÷µÄ¹¦ÄÜÖ÷ÒªÊÇÔÚActionµÄexecute(¼ÙÉèÅäÖÃÎļþÖÐûÓÐÖ¸¶¨)·½·¨Ö®Ç°Ö´ÐÐһЩҵÎñÂß¼£¬Èç¹ûÄã¸ÕºÃÓÐÕâÑùµÄÐèÇ󣬸ÃÀ¹½ØÆ÷ÊÇÄãºÜºÃµÄÑ¡Ôñ£¬ÒªÊ¹¸ÃÀ¹½ØÓÐЧ£¬ActionҪʵÏÖPreparable½Ó¿Ú£¬ÓëÇ°Ã漸¸öÀ¹½ØÆ÷²»Í¬µÄÊǸÃÀ¹½ØÆ÷¼Ì³Ð×ÔMethodFilterInterceptor¶ø²»ÊÇÖ±½Ó¼Ì³Ð×ÔAbstractInterceptor£¬MethodFilterInterceptor¿ÉÒÔ¶ÔÀ¹½ØÆ÷µÄ·½·¨½øÐйýÂË¡£
MethodFilterInterceptorÀ¹½ØÆ÷½«ÔÚAnnotationValidationInterceptorÀ¹½ØÆ÷ʱһÆ𽲽⣬ÔÝʱ¾Í¿ÉÒÔÀí½âΪÓëÖ±½Ó¼Ì³Ð×ÔAbstractInterceptorÎÞÒ죬¸ÃÀ¹½ØÆ÷ÖеÄdoIntercept·½·¨Àí½âΪÒÔÇ°µÄintercept·½·¨£¬ÊÇ´¦Àí¹¦ÄÜÂß¼µÄµØ·½¡£ÏÂÃæÊÇdoIntercept·½·¨µÄÔ´Â룺
±í1
@Override public String doIntercept(ActionInvocation invocation) throws Exception { Object action = invocation.getAction();
if (action instanceof Preparable) {//Åжϵ±Ç°ActionÊÇ·ñʵÏÖÁËPreparable½Ó¿Ú try {//Èç¹ûʵÏÖÁ˵÷ÓÃǰ׺·½·¨ PrefixMethodInvocationUtil.invokePrefixMethod(invocation, new String[]{PREPARE_PREFIX, ALT_PREPARE_PREFIX}); } catch (InvocationTargetException e) { LOG.warn("an exception occured while trying to execute prefixed method", e); } catch (IllegalAccessException e) { LOG.warn("an exception occured while trying to execute prefixed method", e); } catch (Exception e) { LOG.warn("an exception occured while trying to execute prefixed method", e); } //ÊÇ·ñ×ÜÊÇÒªÖ´ÐÐprepare·½·¨£¬alwaysInvokePrepareµÄĬÈÏֵΪtrue if (alwaysInvokePrepare) { ((Preparable) action).prepare(); } } return invocation.invoke();//µ÷ÓÃÏÂÒ»¸öÀ¹½ØÆ÷ } |
¸Ã·½·¨ÖÐÂß¼ºÜ¼òµ¥£¬Ö÷ÒªÂß¼ÔÚPrefixMethodInvocationUtil.invokePrefixMethod·½·¨ÖУ¬Ö´ÐÐʱ°ÑActionInvocation¶ÔÏóÓëÁ½¸öǰ׺×Ö·û´®·ÅÔÚÒ»¸öÊý×éÖд«¸øÁ˸÷½·¨£¬Á½¸öǰ׺·Ö±ðÊÇ"prepare"£¬"prepareDo"£¬ÕâÁ½¸öÖµÊÇÔÚ¸ÃÀ¹½ØÆ÷ÖÐдËÀÁ˵ġ£ÏÖÔÚÈ¥¿´Ò»ÏÂÕâ¸ö·½·¨µÄÔ´Â룺
±í2
public static void invokePrefixMethod(ActionInvocation actionInvocation, String[] prefixes) throws InvocationTargetException, IllegalAccessException { Object action = actionInvocation.getAction();//»ñÈ¡µ±Ç°Ö´ÐеÄAction //»ñÈ¡ActionÒªÖ´Ðеķ½·¨Ãû³Æ String methodName = actionInvocation.getProxy().getMethod(); //Èç¹û·½·¨ÃûΪnullÔò˵Ã÷ÔÚstruts2µÄÅäÖÃÎļþÖÐûÓÐÅäÖÃActionÒªÖ´Ðеķ½·¨Ãû³Æ if (methodName == null) { // Èç¹ûΪnullÔò±íʾִÐÐexecute·½·¨£¬DEFAULT_INVOCATION_METHODNAMEÖµ¾ÍÊÇ"execute" methodName = DEFAULT_INVOCATION_METHODNAME; } //»ñȡǰ׺·½·¨ Method method = getPrefixedMethod(prefixes, methodName, action); if (method != null) {//Èç¹û²»ÎªnullÔòµ÷ÓÃǰ׺·½·¨ method.invoke(action, new Object[0]); } } |
ÏÂÃæÎÒÃÇÈ¥¿´Ò»ÏÂgetPrefixedMethod(prefixes,methodName, action);£¬¿´ËüÊÇÈçºÎ»ñȡǰ׺·½·¨µÄ£º
±í3
public static Method getPrefixedMethod(String[] prefixes, String methodName, Object action) { assert(prefixes != null); //°Ñ·½·¨Ãû³ÆµÄµÚÒ»¸ö×Öĸ´óд²¢·µ»Ø£¬Õâ¸ö¿ÉÒÔ×Ô¼º²Î¿´Ò»ÏÂÔ´Âë String capitalizedMethodName = capitalizeMethodName(methodName); for (String prefixe : prefixes) {//µü´úǰ׺Êý×é //ÕâÀï¾ÍÊǽ«ÒªÖ´Ðеķ½·¨ÃûÊ××Öĸ´óºóÇ°Ãæ¼ÓÉÏÏàÓ¦µÄǰ׺(prepare»òprepareDo) String prefixedMethodName = prefixe + capitalizedMethodName; try {//·µ»Ø¸Ã·½·¨£¬prepare»òprepareDoÖпÉÓÐÒ»¸öÉúЧ return action.getClass().getMethod(prefixedMethodName, EMPTY_CLASS_ARRAY); } catch (NoSuchMethodException e) { if (LOG.isDebugEnabled()) { LOG.debug("cannot find method [" + prefixedMethodName + "] in action [" + action + "]"); } } } return null; } |
µ½ÕâÀïǰ׺·½·¨µÄ»ñÈ¡Óëµ÷Óùý³ÌÒѾ½²½âÍê³É£¬È»ºó¾ÍÊÇÅжÏÊÇ·ñ×ÜÊÇÒªÖ´ÐÐprepare·½·¨£¬Èç¹ûalwaysInvokePrepareµÄֵΪtrueÔò»áÖ´ÐÐActionµÄprepare·½·¨£¬prepare·½·¨¾ÍÊÇÔÚPreparable½Ó¿ÚÖÐÉùÃ÷µÄ·½·¨¡£