创建一个简单的接口
package com.springboot.chapter4.service;
public interface HelloService {
void sayHello(String name);
}
HelloService的实现类
package com.springboot.chapter4.service.impl;
import com.springboot.chapter4.service.HelloService;
public class HelloServiceImpl implements HelloService {
@Override
public void sayHello(String name) {
if(name == null || name.trim() == ""){
throw new RuntimeException("parameter is null");
}
System.out.println(name + "Hello");
}
}
拦截器接口
package com.springboot.chapter4.intercept;
import java.lang.reflect.InvocationTargetException;
import com.springboot.chapter4.invoke.Invocation;
public interface Interceptor {
// 事前方法
public boolean before();
// 事后方法
public void after();
/**
* 取代原有事件方法
* @param invocation -- 回调参数,可以通过它的proceed方法,回调原有事件
* @return 原有事件返回对象
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public Object around(Invocation invocation)
throws InvocationTargetException, IllegalAccessException;
// 事后返回方法。事件没有发生异常执行
public void afterReturning();
// 事后异常方法,当事件发生异常后执行
public void afterThrowing();
// 是否使用around方法取代原有方法
boolean useAround();
}
package com.springboot.chapter4.invoke;
import lombok.Data;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@Data
public class Invocation {
private Object[] params;
private Method method;
private Object target;
public Invocation(Object target, Method method, Object[] params) {
this.target = target;
this.method = method;
this.params = params;
}
// 反射方法
public Object proceed() throws
InvocationTargetException, IllegalAccessException {
return method.invoke(target, params);
}
}
自己的拦截器
package com.springboot.chapter4.intercept;
import com.springboot.chapter4.invoke.Invocation;
import java.lang.reflect.InvocationTargetException;
public class MyInterceptor implements Interceptor {
@Override
public boolean before() {
System.out.println("before ......");
return true;
}
@Override
public boolean useAround() {
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 obj = invocation.proceed();
System.out.println("around after ......");
return obj;
}
@Override
public void afterReturning() {
System.out.println("afterReturning......");
}
@Override
public void afterThrowing() {
System.out.println("afterThrowing......");
}
}
实现ProxyBean
package com.springboot.chapter4.proxy;
import com.springboot.chapter4.intercept.Interceptor;
import com.springboot.chapter4.invoke.Invocation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyBean implements InvocationHandler {
private Object target = null;
private Interceptor interceptor = null;
/**
* 绑定代理对象
* @param target 被代理对象
* @param interceptor 拦截器
* @return 代理对象
*/
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;
}
/**
* 处理代理对象方法逻辑
* @param proxy 代理对象
* @param method 当前方法
* @param args 运行参数
* @return 方法调用结果
* @throws Throwable 异常
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) {
// 异常标识
boolean exceptionFlag = false;
this.interceptor.before();
Invocation invocation = new Invocation(target, method, args);
Object retObj = null;
try {
if (this.interceptor.useAround()) {
retObj = this.interceptor.around(invocation);
} else {
retObj = method.invoke(target, args);
}
} catch (Exception ex) {
// 产生异常
exceptionFlag = true;
}
this.interceptor.after();
if (exceptionFlag) {
this.interceptor.afterThrowing();
} else {
this.interceptor.afterReturning();
return retObj;
}
return null;
}
}
测试
public static void main(String[] args){
HelloService helloService = new HelloServiceImpl();
// 按约定获取proxy
HelloService proxy = (HelloService) ProxyBean.getProxyBean(
helloService, new MyInterceptor());
proxy.sayHello("zhangsan");
System.out.println("#########################################");
proxy.sayHello(null);
}
结果
before ......
around before ......
zhangsanHello
around after ......
after ......
afterReturning......
#########################################
before ......
around before ......
after ......
afterThrowing......
Process finished with exit code 0