import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; interface HelloService { void sayHello(); } class HelloServiceImpl implements HelloService { @Override public void sayHello() { System.out.println("Hello, world!"); } } class MyInvocationHandler implements InvocationHandler { public MyInvocationHandler(Object target) { this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("Before method execution"); System.out.println("After method execution"); return result; } } public class DynamicProxyExample { public static void main(String[] args) { HelloService helloService = new HelloServiceImpl(); InvocationHandler invocationHandler = new MyInvocationHandler(helloService); HelloService proxy = (HelloService) Proxy.newProxyInstance( helloService.getClass().getClassLoader(), helloService.getClass().getInterfaces(), invocationHandler); proxy.sayHello(); } } Complete programming code and related configurations:


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