可以根据拦截器Interceptor 接口的定义开发一个属于自己的拦截器MyInterceptor。
public static Object newProxyInstance(ClassLoader loader, Class[] interfaces,
InvocationHandler h)
. classLoader---类加载器;
. interfaces---绑定的接口,也就是吧代理对象绑定到哪些接口下,可以试多个;
.InvocationHandler---绑定代理对象逻辑实现。
public interface HelloService {
public void sayHello(String name);
}
public class HelloServiceImpl implements HelloService {
@Override
public void sayHello(String name) {
if (Objects.isNull(name) && name.isEmpty()) {
throw new RuntimeException("parameter is null");
}
System.out.println("hello" + name);
}
}
public interface Interceptor {
public boolean before();
public void after();
public Object around(Invocation invocation) throws InvocationTargetException, IllegalAccessException;
public void afterReturning();
public void afterThrowing();
boolean userAround();
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Invocation {
private Object[] params;
private Method method;
private Object target;
public Invocation(Object target, Method method,Object[] params ) {
this.params = params;
this.method = method;
this.target = target;
}
public Object proceed() throws InvocationTargetException, IllegalAccessException {
return method.invoke(target,params);
}
}
import java.lang.reflect.InvocationTargetException;
public class MyInterceptor implements Interceptor {
@Override
public boolean before() {
System.out.println("before……………………");
return true;
}
@Override
public void after() {
System.out.println("after…………………………");
}
@Override
public Object around(Invocation invocation) throws InvocationTargetException, IllegalAccessException {
System.out.println("around before……………………");
Object object = invocation.proceed();
System.out.println("around after…………………………");
return object;
}
@Override
public void afterReturning() {
System.out.println("afterReturning………………");
}
@Override
public void afterThrowing() {
System.out.println("afterReturning………………");
}
@Override
public boolean userAround() {
return true;
}
}
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;
import java.lang.reflect.Method;
public class ProxyBean implements InvocationHandler {
private Object target = null;
private Interceptor interceptor = null;
public static Object getProxyBean(Object target, Interceptor interceptor) {
ProxyBean proxyBean = new ProxyBean();
proxyBean.target = target;
proxyBean.interceptor = interceptor;
Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), proxyBean);
return proxy;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
boolean exceptionFlag = false;
Invocation invocation = new Invocation(target, method, args);
this.interceptor.before();
Object object = null;
try {
if (this.interceptor.userAround()) {
object = this.interceptor.around(invocation);
} else {
object = method.invoke(target, args);
}
} catch (Exception e) {
exceptionFlag = true;
}
this.interceptor.after();
if(exceptionFlag){
this.interceptor.afterThrowing();
}else {
this.interceptor.afterReturning();
return object;
}
return null;
}
}
@Test
void contextLoads() {
HelloService helloService = new HelloServiceImpl();
HelloService proxy = (HelloService) ProxyBean.getProxyBean(helloService, new MyInterceptor());
proxy.sayHello("mazi");
System.out.println("……#name is null !!!……");
proxy.sayHello(null);
}