`
liyixing1
  • 浏览: 961986 次
  • 性别: Icon_minigender_1
  • 来自: 江西上饶
社区版块
存档分类
最新评论

使用p.cglib监控system.out输出流

阅读更多
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);
}
}
分享到:
评论

相关推荐

    cglib.jar/cglib-nodep.jar免费下载

    System.out.println("MyClass's method called"); } } ``` 以上代码演示了如何使用CGLib创建一个动态代理对象,并在方法调用前后添加自定义逻辑。 **6. 应用场景** - AOP框架:Spring AOP就利用了CGLib来实现非...

    cglibjar包

    System.out.println("Method called in target class"); } } } ``` 在这个例子中,我们创建了一个Enhancer实例,设置了要代理的类,并实现了MethodInterceptor。当调用代理对象的方法时,会在执行前后打印出相应...

    Cglib的jar文件 Cglib.zip

    System.out.println("Before method execution"); Object result = proxy.invokeSuper(obj, args); System.out.println("After method execution"); return result; } }); // 创建代理对象 MyTargetClass ...

    java动态代理(JDK和cglib)共10页.pdf.z

    System.out.println("Service is executing..."); } } ``` 我们可以创建一个`InvocationHandler`实现类来添加额外的行为: ```java public class LoggingInvocationHandler implements InvocationHandler { ...

    CGLIB demo

    System.out.println("Before method execution"); Object result = proxy.invokeSuper(obj, args); System.out.println("After method execution"); return result; } }); // 创建代理对象 MyClass ...

    cglib-3.2.4.jar

    System.out.println("方法调用前"); // 调用原始方法 Object result = proxy.invokeSuper(obj, args); // 方法调用后 System.out.println("方法调用后"); return result; } } ``` 五、CGlib与Java反射动态...

    cglib-2.2.2.rar

    System.out.println("Before method execution"); Object result = proxy.invokeSuper(obj, args); System.out.println("After method execution"); return result; } }); // 创建代理对象 MyClass ...

    Java动态代理Proxy和cglib

    MyInterface target = () -> System.out.println("Doing something"); MyInterface proxy = (MyInterface) Proxy.newProxyInstance( MyInterface.class.getClassLoader(), new Class[]{MyInterface.class}, new...

    CGlib动态代理的好例子!

    System.out.println("方法调用前的增强操作"); // 调用原始方法 Object result = proxy.invokeSuper(obj, args); // 在方法调用后执行的逻辑 System.out.println("方法调用后的增强操作"); return result; ...

    Java JDK代理、CGLIB、AspectJ代理分析比较

    System.out.println("doing"); return a + b; } } public static class CalculatorProxy implements Calculator { private Calculator calculator; public CalculatorProxy(Calculator calculator) { this....

    spring-demo20-CGLIB动态代理.zip

    System.out.println("方法执行前的日志"); // 调用原方法 Object result = joinPoint.proceed(); // 方法执行后的日志 System.out.println("方法执行后的日志"); return result; } } ``` 最后,启动...

    java动态代理(JDK和cglib).docx

    System.out.println("Doing something..."); } } class LoggingInvocationHandler implements InvocationHandler { private final Service service; public LoggingInvocationHandler(Service service) { ...

    示例代码:java动态代理和cglib代理的简单例子

    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("预订场地.............."); System.out.println("预订时间.............."); // 调用目标方法 Object returnValue = method.invoke(yeWu, args); // 结算费用 System.out.println("结算...

    16-Java代理模式的学习笔记1

    System.out.println("Developing..."); } } // 代理类 class Proxy implements Programmer { private Programmer programmer; public Proxy(Programmer programmer) { this.programmer = programmer; } @...

    aop的四种实现方式

    method.insertBefore("{ System.out.println(\"Before method execution\"); }"); ctClass.writeFile(); ``` ### 2. CGLIB **CGLIB** (Code Generation Library) 是另一个常用的Java字节码库,它被广泛用于Spring ...

    java 拦截器实现

    System.out.println("Doing something..."); } } class MyInvocationHandler implements InvocationHandler { private final MyService target; public MyInvocationHandler(MyService target) { this....

    Java 动态代理详解(代理模式+静态代理+JDK动态代理+CGLIB动态代理)

    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())); } } ``` 静态代理的优点是可以...

    java dtdl 动态代理

    System.out.println("Before executing method " + method.getName()); method.invoke(realService, args); System.out.println("After executing method " + method.getName()); return null; // 如果方法有...

Global site tag (gtag.js) - Google Analytics