为了更好地理解Spring的AOP,我们先了解下什么是约定编程。
约定编程就是将内容织入到约定的流程中。
对于约定编程,首先你需要记住得是约定的流程是什么,然乎就可以完成对应的任务,却不需要知道底层设计者是怎么将约定的内容织入到对应的流程中的。
为了理解,我们开始举一个简单的约定编程例子。
我们先定义一个简易的接口
接口HelloService
package com.lay.springboot_aop.service; public interface HelloService { public void sayHello(String name); }接口定义了一个sayHello方法,参数是name,这样它就有了一个sayHello方法。
实现类HelloServiceImpl
package com.lay.springboot_aop.service.impl; import com.lay.springboot_aop.service.HelloService; public class HelloServiceImpl implements HelloService{ @Override public void sayHello(String name) { if(name==null&&name.trim()=="") { throw new RuntimeException("name is null!!!"); } System.out.println("hello "+name); } }实现类将sayHello方法进行实现,对方法的参数name判断是否为空。如果为空,则抛出异常。如果不为空,则打印sayHello。
我们直接调用这个方法并且参数不为null和空,控制台将会打印hello和你的参数。但是现在我们想对sayHello方法进行一个拦截,再方法的前后再增加一些方法,这些增加的方法和原方法sayHello组成一套约定的流程。对sayHello方法拦截后会返回一个HelloService类的代理对象proxy,再使用代理对象proxy对方法sayHello进行调用时,程序会根据这套流程去走。
下面我们将分别定义约定的流程和如何将服务类和拦截的方法织入到我们约定的流程并返回代理对象。
在约定流程之前,我们先定义一个拦截器的接口,接口定义了一些方法
内容接口Interceptor
package com.lay.springboot_aop.intercept; import java.lang.reflect.InvocationTargetException; import com.lay.springboot_aop.invoke.Invocation; public interface Interceptor { //前方法 public boolean before(); //后方法 public void after(); /** * 取代原有事件的方法 * @param invocation 回调参数,可以通过它的proceed方法,回调原有事件 * @return 原有事件的返回对象 * @throws InvocationTargetException * @throws IllegalAccessException * @Date 2018年10月23日 下午4:12:13 * @Author lay */ public Object around(Invocation invocation) throws InvocationTargetException,IllegalAccessException; //是否返回方法。事件没有发生异常执行 public void afterReturning(); //事后异常方法,当事件发生异常时执行 public void afterThrowing(); //是否使用around()方法取代原有方法 boolean useAround(); }其中around方法里有个参数invocation,主要用于回调原有的方法。
Invocation类
package com.lay.springboot_aop.invoke; 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.target=target; this.method=method; this.params=params; } //反射方法 public Object proceed() throws InvocationTargetException,IllegalAccessException{ return method.invoke(target, params); } public Object[] getParams() { return params; } public void setParams(Object[] params) { this.params = params; } public Method getMethod() { return method; } public void setMethod(Method method) { this.method = method; } public Object getTarget() { return target; } public void setTarget(Object target) { this.target = target; } }其中的proceed方法,它会以反射的形式去调用原有的方法。
接着我们去实现内容接口MyInterceptor
实现类MyInterceptor
package com.lay.springboot_aop.intercept.impl; import java.lang.reflect.InvocationTargetException; import com.lay.springboot_aop.intercept.Interceptor; import com.lay.springboot_aop.invoke.Invocation; 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 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; } }当调用proxy代理对象方法后(后面会讲获取代理对象proxy,这里主要看流程的定义,我们先假设拿到了代理对象,意味者对方法拦截成功),我们定义流程如下。
(1)使用proxy调用方法时会先执行拦截器的before方法。
(2)如果拦截器的useAround方法返回true,则执行拦截器的around方法,而不调用target原对象对应的方法,但around方法的参数invoaiton对象存在一个proceed方法,它额可以滴哦啊用target对象对应的方法;如果useAround方法返回false,则直接调用target对象的事件方法。
(3)无论怎么样,在完成之前的事情后,都会执行拦截器的after方法。
(4)在执行around方法或者回调target的事件方法时,可能发生异常,也可能不发生异常。如果发生异常,就执行拦截器的afterThrowing方法,否则就执行afterReturning方法。
具体流程图如下:
我们先理解一下什么是动态代理。例如,当你需要采访一名儿童时,首先需要经过他父母的同意,在一些问题上父母会替他回答,而另外一些问题,父母觉得不太适合这个小孩会替孩子拒绝掉,显然父母就是这名儿童的代理(proxy)。通过代理可以增强或者控制对真实对象(target)的访问。
JDK中提供了类Proxy的静态方法newPorxyInstance来生成一个代理对象。
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException它有三个参数:
ClassLoader loader: 类加载器Class<?>[] interfaces:绑定的接口,也就是把代理对象绑定到哪些接口下,可以时多个。InvocationHandler h:绑定代理对象逻辑实现这里InvocationHandler 时一个接口,它定义了一个invoke方法,这个方法就是实现代理对象的逻辑实现的,其源码如下:
public interface InvocationHandler { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable; }这里通过目标对象(target)、方法(method)和参数(args)就可以反射方法运行了。
为了将服务类和拦截方法织入对应的流程,并且返回代理对象。这里编写了一个ProxyBean类,
ProxyBean
package com.lay.springboot_aop.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import com.lay.springboot_aop.intercept.Interceptor; import com.lay.springboot_aop.invoke.Invocation; public class ProxyBean implements InvocationHandler{ private Object target=null; private Interceptor interceptor=null; /** * 绑定代理对象 * @param target 被代理对象 * @param interceptor 拦截器 * @return 代理对象 * @Date 2018年10月23日 下午4:56:57 * @Author lay */ 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); //Object proxy=Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getClasses(), proxyBean); //返回代理对象 return proxy; } /** * 处理代理对象方法逻辑 * @param proxy 代理对象 * @param method 当前方法 * @param args 运行参数 * @return 方法调用结果 * @throws Throwable 异常 * @Date 2018年10月23日 下午4:56:57 * @Author lay */ @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 e){ //异常 exceptionFlag=true; } this.interceptor.after(); if(exceptionFlag) { this.interceptor.afterThrowing(); }else { this.interceptor.afterReturning(); return retObj; } return null; } }首先,这个ProxyBean实现了接口InvocationHandler,因此就可以定义invoke方法了。其中在getBean方法中,我们让其盛恒了一个代理对象,并且创建了一个proxyBean实例保存目标对象(target)和拦截器(interceptor),为后面的调用做好准备。其次,生成了一个代理对象,而这个代理对象挂在target实现的接口下,所以可以用target对象实现的接口对这个代理对象实现强制类型转换,并且将这个代理对象的逻辑挂在ProxyBean实例下,这样就完成了目标对象target和代理对象proxy的绑定。最后,将代理对象返回给调用者。
测试
package com.lay.springboot_aop.test; public class Test { public static void main(String[] args) { testProxy(); } private static void testProxy() { HelloService helloService=new HelloServiceImpl(); //按照约定获取proxy HelloService proxy=(HelloService)ProxyBean.getProxyBean(helloService,new MyInterceptor()); System.out.println("\n################ invoke method sayHello ##########################\n"); proxy.sayHello("Lay"); System.out.println("\n################ invoke method sayHello param name is null!##########################\n"); proxy.sayHello(null); } }结果
################ invoke method sayHello ########################## before-------------- around before-------------- hello Lay around after-------------- after-------------- afterReturning-------------- ################ invoke method sayHello param name is null!########################## before-------------- around before-------------- after-------------- afterThrowing--------------我们自己写了一个约定编程的例子,说明只要提供一定的约定规则,按照约定进行编程后,就可以把自己开发的代码织入约定的流程中。而实际开发中,我们只需要知道框架和你的约定便可以了。Spring Aop就是这么做的。它可以通过与我们约定,把对应的方法通过动态代理技术织入约定的流程中,这就是Spring Aop编程的本质。所以掌握Spring AOP的根据在于掌握其对我们的约定规则。