2019独角兽企业重金招聘Python工程师标准>>>
package com.springmvc.test;
public class Base {
public void add(){
System.out.println("add ----- ");
}
}
package com.springmvc.test;
import java.lang.reflect.Method;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
public class CglibProxy implements MethodInterceptor{
@Override
public Object intercept(Object object, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
System.out.println("before--------");
proxy.invokeSuper(object, args);
System.out.println("after--------");
return null;
}
}
package com.springmvc.test;
import org.springframework.cglib.proxy.Enhancer;
public class Factory {
public static Base getInstance(CglibProxy proxy){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Base.class);
enhancer.setCallback(proxy);
Base base = (Base) enhancer.create();
return base;
}
}
package com.springmvc.test;
public class Test01 {
public static void main(String[] args) {
CglibProxy proxy = new CglibProxy();
Base base = Factory.getInstance(proxy);
base.add();
}
}