package com.jiaoyiping.util.demo; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * Created with IntelliJ IDEA. * User: 焦一平 * Date: 2015/7/8 * Time: 10:54 * To change this template use File | Settings | File Templates. * JDK动态代理示例 */ public class JDKDynamicProxyDemo { public static void main(String[] args) { Business business = new BusinessImpl(); Business proxy = (Business)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{Business.class}, new BusinessProxy(business)); proxy.doBusiness(); } } interface Business { void doBusiness(); } class BusinessImpl implements Business { @Override public void doBusiness() { System.out.println("执行业务方法"); } } class BusinessProxy implements InvocationHandler { private Business business; public BusinessProxy(Business business) { this.business = business; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("before invoke"); Object result = method.invoke(business, args); System.out.println("after invoke"); return result; } }