一个实现类
public class App {
public static void main(String[] args) {
SomeBussiness someBussiness = (SomeBussiness)Interceptor.createProxy(SomeBussiness.class);
someBussiness.doSomething();
}
}
一个MethodInterceptor
public class Interceptor implements MethodInterceptor {
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("Interceptor---------intercept------before");
Object o = proxy.invokeSuper(obj,args);
System.out.println("Interceptor---------intercept------after");
return o;
}
public static Object createProxy(Class supperClass){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(supperClass);
enhancer.setCallback(new Interceptor());
return enhancer.create();
}
}
一个测试类
public class App {
public static void main(String[] args) {
SomeBussiness someBussiness = (SomeBussiness)Interceptor.createProxy(SomeBussiness.class);
someBussiness.doSomething();
}
}