import net.bytebuddy.ByteBuddy; import net.bytebuddy.implementation.MethodDelegation; import net.bytebuddy.matcher.ElementMatchers; public class DynamicProxyExample { public static void main(String[] args) throws Exception { String proxyResult = new ByteBuddy() .subclass(String.class) .method(ElementMatchers.named("toUpperCase")) .intercept(MethodDelegation.to(MyInterceptor.class)) .make() .load(Thread.currentThread().getContextClassLoader()) .getLoaded() .newInstance() .toUpperCase(); System.out.println(proxyResult); } public static class MyInterceptor { public static String intercept() { System.out.println("Before invoking the method"); String result = "Hello, Byte Buddy!"; System.out.println("After invoking the method"); return result; } } } Before invoking the method After invoking the method HELLO, BYTE BUDDY! <dependencies> <dependency> <groupId>net.bytebuddy</groupId> <artifactId>byte-buddy</artifactId> <version>1.11.12</version> </dependency> </dependencies>


上一篇:
下一篇:
切换中文