方法(1)
创建对象之后不直接调用方法,通过反射找到方法,通过invoke调用方法,这样可以在方法前面加上和后面你的语句,被调用的类不用作任何修改
方法(2)
实现一个这个类的子类,子类实现全部的父类的方法,在每个方法里面加上你的语句,然后调用父类的同名方法:super.xxx();最后再加上你的结束语句。

解决方案 »

  1.   

    按照楼主的要求,一楼的方法一应该最适合,我就不重复了哈
    这是jpetstore里面的代码,请参考------------------------------------------------------------------
    public class BeanAction extends Action {
      private static final String NO_METHOD_CALL = "*";
      private static final String SUCCESS_FORWARD = "success";  public final ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String forward = SUCCESS_FORWARD;
        try {
          if (!(form instanceof BaseBean)) {
            if (form != null) {
              throw new BeanActionException("The form for mapping '" + mapping.getPath() + "' named '" + mapping.getName() + "' was not an instance of BaseBean.  BeanAction requires an BaseBean instance.");
            } else {
              throw new BeanActionException("The form for mapping '" + mapping.getPath() + "' named '" + mapping.getName() + "' was null.  BeanAction requires an BaseBean instance.");
            }
          }
          BaseBean bean = (BaseBean) form;
          ActionContext.initCurrentContext(request, response);
          if (bean != null) {
            // Explicit Method Mapping        Method method = null;
            String methodName = mapping.getParameter();
            if (methodName != null && !NO_METHOD_CALL.equals(methodName)) {
              try {
                method = bean.getClass().getMethod(methodName, null);
                synchronized (bean) {
                  forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
                }
              } catch (Exception e) {
                throw new BeanActionException("Error dispatching bean action via method parameter ('" + methodName + "').  Cause: " + e, e);
              }
            }        // Path Based Method Mapping        if (method == null && !NO_METHOD_CALL.equals(methodName)) {
              methodName = mapping.getPath();
              if (methodName.length() > 1) {
                int slash = methodName.lastIndexOf("/") + 1;
                methodName = methodName.substring(slash);
                if (methodName.length() > 0) {
                  try {
                    method = bean.getClass().getMethod(methodName, null);
                    synchronized (bean) {
                      forward = bean.getInterceptor().intercept(new ActionInvoker(bean, method));
                    }
                  } catch (Exception e) {
                    throw new BeanActionException("Error dispatching bean action via URL pattern ('" + methodName + "').  Cause: " + e, e);
                  }
                }
              }
            }
          }
        } catch (Exception e) {
          forward = "error";
          request.setAttribute("BeanActionException", e);
        }
        return mapping.findForward(forward);
      }
    }
    ---------------------------------------------------------------------------
    package org.apache.struts.beanaction;import java.lang.reflect.Method;public class ActionInvoker {
      private Method method;
      private BaseBean bean;  public ActionInvoker(BaseBean bean, Method method) {
        this.method = method;
        this.bean = bean;
      }  public String invoke() {
        try {
          return (String) method.invoke(bean, null);
        } catch (Exception e) {
          throw new BeanActionException("Error invoking Action.  Cause: " + e, e);
        }
      }
    }
      

  2.   

    方法(1)
    创建对象之后不直接调用方法,通过反射找到方法,通过invoke调用方法,这样可以在方法前面加上和后面你的语句,被调用的类不用作任何修改------------------------------------------------------------------------------------
    这个方法我也想过,不过太笨重.我需要创建对象之后直接调用自己的方法,另一个类马上就知道
    ------------------------------------------------------------------------------------方法(2)
    实现一个这个类的子类,子类实现全部的父类的方法,在每个方法里面加上你的语句,然后调用父类的同名方法:super.xxx();最后再加上你的结束语句。
    -----------------------------------------------------------------------------------
    这个不够灵活 
    还有其它办法吗 ?
      

  3.   

    1.不知道java有没有回调函数。
    2.可以弄一个静态全局变量,在你的方法执行的时候去改变这个变量。另外的类用线程的方式监视这个全局变量的变化。
      

  4.   

    其实SPRING的AOP的拦戴BEAN就有点类似楼主所提出的问题!嗯,高手真多,这些方法都妙呀!
      

  5.   

    定义一个接口,一个子类并实现这个接口。
    把通知 另一个类什么的。。你想做的任何事,都放在接口实现里。定义一个事件类,通过事件类来调用该子类,通过事件类来控制什么调用方法前,调用方法后之类的东西。。class subA extends A implements yourimp;class eventA {
      yourimp s;
      public EventNotifier (yourimp s1){
        s= s1;
      }
      otherfun....
    }class callA{
      void fun(){
         subA sa = new subA();
         eventA e = new eventA();
         e.EventNotifier(sa);
        
         ...anything you want to do..
      }
    }我觉得这样是完全可以事件楼主所说的功能的。。用接口来模拟回调函数。。
    而且很灵活
      

  6.   

    以上的方法我感觉和以下的方法没有什么不同,还是不够灵活,有没有其它更好的办法(1)创建对象之后不直接调用方法,通过反射找到方法,通过invoke调用方法,这样可以在方法前面加上和后面你的语句,被调用的类不用作任何修改
      

  7.   

    Rangiggs(刘德华) ( ) 信誉:83 
    我靠,刘德华,楼主没性欲!!!
      

  8.   

    终于搞定了这个问题,有兴趣的朋友可以查一下jdk doc 
    java.lang.reflect.InvocationHandler 接口 
    java.lang.reflect.Proxy 类 
    还不明白的话可以和我交流 [email protected]