public interface A { public void aa(); }
public interface B { public void bb(); }
public class ABImpl implements A, B{ public void aa() { System.out.println("aaaa"); } public void bb() { System.out.println("bb"); } }
public class Logger { public void logging() { System.out.println("logging"); } }
public class JDKProxyMultyTest implements InvocationHandler{ private Object target; private Logger logger; public JDKProxyMultyTest(Object target, Logger logger) { super(); this.target = target; this.logger = logger; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { this.logger.logging(); method.invoke(target, args); return null; } }
@Test public void test1(){ Object target = new ABImpl(); Logger logger = new Logger(); JDKProxyMultyTest jdkProxyMultyTest = new JDKProxyMultyTest(target, logger); A proxyA = (A) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), jdkProxyMultyTest); proxyA.aa(); B proxyB = (B) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), jdkProxyMultyTest); proxyB.bb(); }
时间: 2024-09-29 05:31:46