import net.bytebuddy.ByteBuddy; import net.bytebuddy.implementation.MethodDelegation; import net.bytebuddy.matcher.ElementMatchers; public class ProxyExample { public static void main(String[] args) throws Exception { Class<?> proxyClass = new ByteBuddy() .subclass(Object.class) .implement(ExampleInterface.class) .method(ElementMatchers.named("doSomething")) .intercept(MethodDelegation.to(ExampleInterceptor.class)) .make() .load(ProxyExample.class.getClassLoader()) .getLoaded(); ExampleInterface proxy = (ExampleInterface) proxyClass.newInstance(); proxy.doSomething(); } } public interface ExampleInterface { void doSomething(); } public class ExampleInterceptor { public static void intercept() { System.out.println("Before method execution"); System.out.println("After method execution"); } }


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