Spring中cglib动态代理
依赖Jar包cglib-3.1-sources.jar下载地址:http://central.maven.org/maven2/cglib/cglib/3.1/cglib-3.1-sources.jar
public class SampleClass { public void test(){ System.out.println("hello world"); } public static void main(String[] args) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(SampleClass.class); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("before method run..."); Object result = proxy.invokeSuper(obj, args); System.out.println("after method run..."); return result; } }); SampleClass sample = (SampleClass) enhancer.create(); sample.test(); } }
设置和被代理对象有相同的父类。
凝一眸碧水,拈一缕清风,于一怀静谧中倾听凡尘的落音。
不再奢求什么,做简单的自己,过简单的生活,心在,梦在,你在,便是光阴赐予我的最美。