方法(1)
创建对象之后不直接调用方法,通过反射找到方法,通过invoke调用方法,这样可以在方法前面加上和后面你的语句,被调用的类不用作任何修改
方法(2)
实现一个这个类的子类,子类实现全部的父类的方法,在每个方法里面加上你的语句,然后调用父类的同名方法:super.xxx();最后再加上你的结束语句。
创建对象之后不直接调用方法,通过反射找到方法,通过invoke调用方法,这样可以在方法前面加上和后面你的语句,被调用的类不用作任何修改
方法(2)
实现一个这个类的子类,子类实现全部的父类的方法,在每个方法里面加上你的语句,然后调用父类的同名方法:super.xxx();最后再加上你的结束语句。
解决方案 »
- 需求管理系统
- hibernate 中获取 jdbc connection
- jsp 中关于Fckeditor 上传的问题 急!!!!
- JavaWeb中常用的WEB服务器有哪些?
- 详讯:甲骨文将以74亿美元收购SUNhttp://www.sina.com.cn 2009年04月20日 20:16 新浪科技
- 各位学长请赐教,概念问题。
- 请问通过profile as运行java程序什么意思啊
- evS1300.ocx 问题
- WebLogic是应用程序服务器吗?
- 请高人给俺写个Applet访问远程计算机*.txt文件的例子,THANK YOU~
- servlet方面的问题
- 请教:在spring+hibernate+struts框架中如何在程序中处理数据库错误
这是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);
}
}
}
创建对象之后不直接调用方法,通过反射找到方法,通过invoke调用方法,这样可以在方法前面加上和后面你的语句,被调用的类不用作任何修改------------------------------------------------------------------------------------
这个方法我也想过,不过太笨重.我需要创建对象之后直接调用自己的方法,另一个类马上就知道
------------------------------------------------------------------------------------方法(2)
实现一个这个类的子类,子类实现全部的父类的方法,在每个方法里面加上你的语句,然后调用父类的同名方法:super.xxx();最后再加上你的结束语句。
-----------------------------------------------------------------------------------
这个不够灵活
还有其它办法吗 ?
2.可以弄一个静态全局变量,在你的方法执行的时候去改变这个变量。另外的类用线程的方式监视这个全局变量的变化。
把通知 另一个类什么的。。你想做的任何事,都放在接口实现里。定义一个事件类,通过事件类来调用该子类,通过事件类来控制什么调用方法前,调用方法后之类的东西。。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..
}
}我觉得这样是完全可以事件楼主所说的功能的。。用接口来模拟回调函数。。
而且很灵活
我靠,刘德华,楼主没性欲!!!
java.lang.reflect.InvocationHandler 接口
java.lang.reflect.Proxy 类
还不明白的话可以和我交流 [email protected]