1.约定编程

2020-08-28  本文已影响0人  LANSHENGYANG

约定编程

约定

public interface HelloService {
    public void sayHello(String name);
}
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("hello " + name);
    }
}
/**
 * 拦截器接口
 */
public interface Interceptor {
    /**
     * 事前方法
     * @return
     */
    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方法取代原有方法
     * @return
     */
    boolean useAround();
}
public class Invocation {
    private Object target;
    private Method method;
    private Object[] params;

    public Invocation(Object target, Method method, Object[] params) {
        this.target = target;
        this.method = method;
        this.params = params;
    }

    /**
     * 反射方法
     *
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public Object proceed() throws InvocationTargetException, IllegalAccessException {
        return method.invoke(target, params);
    }
}
public class MyInterceptor implements Interceptor {
    @Override
    public boolean before() {
        System.out.println("before....");
        return false;
    }

    @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....");
    }

    @Override
    public boolean useAround() {
        return true;
    }
}
HelloService helloService = new HelloServiceImpl();
HelloService proxy = (HelloService) ProxyBean.getProxyBean(helloService, new MyInterceptor());
public class testProxy {
    public static void main(String[] args) {
        HelloService helloService = new HelloServiceImpl();
        HelloService proxy = (HelloService) ProxyBean.getProxyBean(helloService, new MyInterceptor());
        proxy.sayHello("zhangsan");
        System.out.println("########### name is null! ###########");
        proxy.sayHello(null);
    }
}
before....
around before....
hello zhangsan
around after....
after....
afterReturning....
########### name is null! ###########
before....
around before....
after....
afterThrowing....

ProxyBean的实现

/**
* 处理代理对象方法逻辑
* @param proxy 代理对象
* @param method 当前方法
* @param args 运行参数
* @return 方法调用结果
*/
publi Object invoke(Object proxy, Metthod method, Object[] args);
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) throws Throwable {
        //异常标识
        boolean exceptionFlag = false;
        Invocation invocation = new Invocation(target, method, args);
        Object retObj = null;
        try {
            if (this.interceptor.before()) {
                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 null;
    }
}
HelloService helloService = new HelloServiceImpl();
//按约定获取
HelloService proxy = (HelloService) ProxyBean.getProxyBean(helloService, new MyInterceptor());

总结

上一篇 下一篇

猜你喜欢

热点阅读