Java 署理 对一批方法 的前后进行特殊处理
Java 代理 对一批方法 的前后进行特殊处理
package proxy2; /** * * 如何对如下方法,统一添加一个前后处理的逻辑呢? * 有静态的、有非静态的?如何统一处理? */ public class BizCls2 { public static void staticMethod1() { System.out.println("处理方法1"); } public static void staticMethod3(String args) { System.out.println("处理方法3"+args); } public void method2(String args) { System.out.println("处理方法2"+args); } }
package proxy2; import org.apache.commons.lang3.reflect.MethodUtils; /** * 静态方法通过硬编码的形式来实现 */ public class BizStaticMethodCaller { public static <T> T callWithLog(Class<?> type, String method, Object... parms) throws Exception { System.out.println("前处理日志打印,参数"+parms); Object ret = MethodUtils.invokeStaticMethod(type, method, parms); System.out.println("后处理日志打印"); return (T) ret; } }
package proxy2; import java.lang.reflect.Method; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; /** * 动态代理只能处理 非静态的方法,非静态的方法通过该形式进行处理 */ public class CglibProxy implements MethodInterceptor { static CglibProxy instence=new CglibProxy(); public static <T> T getProxyInstance(Class<T> type) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(type); enhancer.setCallback(instence); return (T) enhancer.create(); } @Override public Object intercept(final Object target, Method method, final Object[] args, final MethodProxy proxy) throws Throwable { System.out.println("前处理日志打印,参数" + args); Object result = proxy.invokeSuper(target, args); System.out.println("后处理日志打印"); return result; } }
package proxy2; /** * * 使用方式 */ public class TestCiglib { public static void main(String[] args) throws Exception { // 静态方法 的调用方式,ide的代码提示、效验、查找引用不太好用了 ,不过也可以对IDE的扩展进行弥补 // 推荐非静态的方法 BizStaticMethodCaller.callWithLog(BizCls2.class, "staticMethod1"); BizStaticMethodCaller.callWithLog(BizCls2.class, "staticMethod3", "333"); // 如何使非静态的方法,统一采用 动态代理的形式,所以对象应有统一的地方创建 BizCls2 hello = CglibProxy.getProxyInstance(BizCls2.class); // 非静态的,通过代理来完成 hello.method2("22"); } }
运行结果:
前处理日志打印,参数[Ljava.lang.Object;@12b6651
处理方法1
后处理日志打印
前处理日志打印,参数[Ljava.lang.Object;@124bbbf
处理方法3333
后处理日志打印
前处理日志打印,参数[Ljava.lang.Object;@1960f05
处理方法222
后处理日志打印
应该还有一种很暴力的方式就是 采用 ASM 对java字节 添加一些额外的代码来实现
google 搜 ASM AOP 很多例子