package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class Test extends PrintStream implements MethodInterceptor {
private File targs;
public Test(File targs) throws FileNotFoundException {
super("D:/a.txt");
this.targs = targs;
}
public Object getTargs() {
return targs;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return method.invoke(targs, args);
}
public void println(Object o) {
System.out.println(o);
}
public void println(String o) {
System.out.println(o);
}
public Object intercept(Object arg0, Method arg1, Object[] arg2,
MethodProxy arg3) throws Throwable {
try {
String s = new String(((String) arg2[0]).getBytes("gbk"), "utf-8");
} catch (Exception e) {
}
return arg3.invokeSuper(arg0, arg2);
}
}
分享到:
相关推荐
System.out.println("MyClass's method called"); } } ``` 以上代码演示了如何使用CGLib创建一个动态代理对象,并在方法调用前后添加自定义逻辑。 **6. 应用场景** - AOP框架:Spring AOP就利用了CGLib来实现非...
System.out.println("Method called in target class"); } } } ``` 在这个例子中,我们创建了一个Enhancer实例,设置了要代理的类,并实现了MethodInterceptor。当调用代理对象的方法时,会在执行前后打印出相应...
System.out.println("Before method execution"); Object result = proxy.invokeSuper(obj, args); System.out.println("After method execution"); return result; } }); // 创建代理对象 MyTargetClass ...
System.out.println("Service is executing..."); } } ``` 我们可以创建一个`InvocationHandler`实现类来添加额外的行为: ```java public class LoggingInvocationHandler implements InvocationHandler { ...
System.out.println("Before method execution"); Object result = proxy.invokeSuper(obj, args); System.out.println("After method execution"); return result; } }); // 创建代理对象 MyClass ...
System.out.println("方法调用前"); // 调用原始方法 Object result = proxy.invokeSuper(obj, args); // 方法调用后 System.out.println("方法调用后"); return result; } } ``` 五、CGlib与Java反射动态...
System.out.println("Before method execution"); Object result = proxy.invokeSuper(obj, args); System.out.println("After method execution"); return result; } }); // 创建代理对象 MyClass ...
MyInterface target = () -> System.out.println("Doing something"); MyInterface proxy = (MyInterface) Proxy.newProxyInstance( MyInterface.class.getClassLoader(), new Class[]{MyInterface.class}, new...
System.out.println("方法调用前的增强操作"); // 调用原始方法 Object result = proxy.invokeSuper(obj, args); // 在方法调用后执行的逻辑 System.out.println("方法调用后的增强操作"); return result; ...
System.out.println("方法执行前的日志"); // 调用原方法 Object result = joinPoint.proceed(); // 方法执行后的日志 System.out.println("方法执行后的日志"); return result; } } ``` 最后,启动...
System.out.println("Doing something..."); } } class LoggingInvocationHandler implements InvocationHandler { private final Service service; public LoggingInvocationHandler(Service service) { ...
System.out.println("Before cglib proxy"); Object result = proxy.invokeSuper(obj, args); System.out.println("After cglib proxy"); return result; } } // 测试类 public class Main { public static ...
System.out.println("doing"); return a + b; } } public static class CalculatorProxy implements Calculator { private Calculator calculator; public CalculatorProxy(Calculator calculator) { this....
System.out.println("预订场地.............."); System.out.println("预订时间.............."); // 调用目标方法 Object returnValue = method.invoke(yeWu, args); // 结算费用 System.out.println("结算...
System.out.println("Developing..."); } } // 代理类 class Proxy implements Programmer { private Programmer programmer; public Proxy(Programmer programmer) { this.programmer = programmer; } @...
method.insertBefore("{ System.out.println(\"Before method execution\"); }"); ctClass.writeFile(); ``` ### 2. CGLIB **CGLIB** (Code Generation Library) 是另一个常用的Java字节码库,它被广泛用于Spring ...
System.out.println("Doing something..."); } } class MyInvocationHandler implements InvocationHandler { private final MyService target; public MyInvocationHandler(MyService target) { this....
System.out.println(String.format("log start time [%s] ", new Date())); } private void after() { System.out.println(String.format("log end time [%s] ", new Date())); } } ``` 静态代理的优点是可以...
System.out.println("Before executing method " + method.getName()); method.invoke(realService, args); System.out.println("After executing method " + method.getName()); return null; // 如果方法有...