标签:org new glib out after super code test reflect
package enhancerTest;/*** Created by LiuSuSu on 2017/3/26.*/public class UserService {public void doxx(){System.out.println("do...");}}
package enhancerTest;import org.springframework.cglib.proxy.MethodInterceptor;import org.springframework.cglib.proxy.MethodProxy;import java.lang.reflect.Method;/*** Created by LiuSuSu on 2017/3/26.*/public class CglibCallBackUserService implements MethodInterceptor {/*** cglib调用的** @param o 调用对象* @param method 调用的方法* @param args 参数* @param methodProxy 代理的方法* @return* @throws Throwable*/@Overridepublic Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {System.out.println("before " + method.getName());//注意invoke代理的是自己,这是一个递归,死循环//Object obj= methodProxy.invoke(o, args);Object obj = methodProxy.invokeSuper(o, args);System.out.println("after " + method.getName());return obj;}}
package enhancerTest;import org.springframework.cglib.proxy.Enhancer;/*** Created by LiuSuSu on 2017/3/26.*/public class EnhanceTest {public static void main(String[] args) {Enhancer enhancer = new Enhancer();enhancer.setSuperclass(UserService.class);enhancer.setCallback(new CglibCallBackUserService());UserService service = (UserService) enhancer.create();service.doxx();System.out.println("over");}}
标签:org new glib out after super code test reflect
原文地址:http://www.cnblogs.com/LiuChunfu/p/6624769.html