spring中AOP实现的原理

来源:互联网 发布:销售pipeline数据 编辑:程序博客网 时间:2024/06/11 04:36

spring中AOP实现的原理

1、什么是AOP?
AOP:全称是Aspect  Oriented  Programming即:面向切面编程。通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。简单的说:它就是把我们程序中重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上,对我们的已有的方法的增强。
2、怎么实现AOP?
AOP的实现方式是使用动态代理的技术。下面我将举一个对service中事务提取出来处理的例子:

其实总的总结一下AOP就是:纵向重复,横向抽取
首先创建一个UserService接口和一个UserServiceImpl实现类
UserService接口
public interface UserService {void save();void find();void delete();void update();}
UserServiceImpl实现类
public class UserServiceImpl implements UserService {@Overridepublic void save() {System.out.println("保存用户");}@Overridepublic void find() {System.out.println("查找用户");}@Overridepublic void delete() {System.out.println("删除用户");}@Overridepublic void update() {System.out.println("更新用户");}}
3、使用JDK官方的Proxy类创建代理对象
原理图:


代理类:
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import com.haha.service.UserService;public class UserServiceProxyFactory implements InvocationHandler{//被代理对象private UserService target;public UserServiceProxyFactory(UserService target) {this.target = target;}//获得被代理对象public UserService getUserService(){//参数1:类加载器=>用来加载动态产生的代理类。//参数2:填写代理类要实现的接口(可以是多个)//参数3:在代理中需要做什么=>接口回调来表示在代理类中要做的功能return (UserService) Proxy.newProxyInstance(UserServiceProxyFactory.class.getClassLoader(),new Class[]{UserService.class}, this);}@Override//参数1:动态代理对象//参数2:当前的业务方法//参数3:业务方法的参数public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("开启事务");//执行方法Object invoke = method.invoke(target, args);System.out.println("提交或者回滚事务");return invoke;}}
测试方法:
@Testpublic void fun(){//创建被代理的对象UserService us=new UserServiceImpl();//创建代理工厂UserServiceProxyFactory factory=new UserServiceProxyFactory(us);//创建代理类UserService userProxy = factory.getUserService();//调用方法测试userProxy.save();}
另外这个代理类比较的局限,只能代理UserService接口,下面是一个泛型类的代理类,可以代理所有类型的service用来事务处理需要的接口。
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class ServiceProxyFactory<T> implements InvocationHandler{private T target;public ServiceProxyFactory(T target) {this.target = target;}public T getProxy(){return (T) Proxy.newProxyInstance(ServiceProxyFactory.class.getClassLoader(), target.getClass().getInterfaces(), this);}@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("开启事务");Object invoke = method.invoke(target, args);System.out.println("提交|回滚事务");return invoke;}}
这种泛型的测试方法:
//泛型的测试实现@Testpublic void fun2(){//创建被代理的对象UserService us=new UserServiceImpl();//创建代理工厂ServiceProxyFactory<UserService> factory=new ServiceProxyFactory<UserService>(us);//创建代理类UserService userProxy = factory.getProxy();//调用方法测试userProxy.save();}
使用jdk的这种代理方式是被代理的类必须是接口,没有那么灵活。
4、使用cglib的Enhancer类创建代理对象
原理图:





cglib这种代理方式是代理类会继承被代理类。
代理类代码:
import org.springframework.cglib.proxy.Enhancer;import org.springframework.cglib.proxy.MethodInterceptor;import org.springframework.cglib.proxy.MethodProxy;import com.haha.service.UserService;import com.haha.service.UserServiceImpl;public class UserServiceProxyFactory2 implements MethodInterceptor{public UserService getUserServiceProxy(){Enhancer enhancer=new Enhancer();//指定代理类的父类(被代理对象)enhancer.setSuperclass(UserServiceImpl.class);//指定代理方法如何实现(接口回调)enhancer.setCallback(this);return (UserService) enhancer.create();}@Override//参数1:代理对象//参数2:当前业务方法//参数3:业务方法参数//参数4:代理类中当前代理方法public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {System.out.println("开启事务");//调用原业务方法Object invokeSuper = methodProxy.invokeSuper(proxy, args);System.out.println("提交|回滚事务");return invokeSuper;}}
测试类:
@Testpublic void fun1(){//创建代理工厂UserServiceProxyFactory2 factory=new UserServiceProxyFactory2();//创建代理类UserService proxy=factory.getUserServiceProxy();//调用方法测试proxy.delete();}
同样,我也写了泛型的代理类
import java.lang.reflect.Method;import org.springframework.cglib.proxy.Enhancer;import org.springframework.cglib.proxy.MethodInterceptor;import org.springframework.cglib.proxy.MethodProxy;public class ServiceProxyFactory2<T>  implements MethodInterceptor{private T target;public ServiceProxyFactory2(T target) {this.target = target;}public T getProxy(){Enhancer enhancer=new Enhancer();enhancer.setSuperclass(target.getClass());enhancer.setCallback(this);return (T) enhancer.create();}@Overridepublic Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {System.out.println("开启事务");//调用原业务方法Object invokeSuper = methodProxy.invokeSuper(proxy, args);System.out.println("提交|回滚事务");return invokeSuper;}}
测试方法:
@Testpublic void fun2(){UserServiceImpl us=new UserServiceImpl();//创建代理工厂ServiceProxyFactory2<UserServiceImpl> factory=new ServiceProxyFactory2<UserServiceImpl>(us);//创建代理类UserService proxy=factory.getProxy();//调用方法测试proxy.delete();}
5、spring-aop开发
准备工作:
导包:

还是使用UserService接口和UserServiceImpl实现类
xml版的代码
通知:
import org.aspectj.lang.ProceedingJoinPoint;public class MyAdvice {//通知类型有五种//前置通知--->在目标方法执行前增强public void before(){System.out.println("我是前置通知");}//环绕通知---->在目标方法之前和之后都增强public Object around(ProceedingJoinPoint pjp) throws Throwable{System.out.println("我是环绕通知,前半部分");//执行业务方法Object proceed=pjp.proceed();System.out.println("我是环绕通知,后半部分");return proceed;}//后置通知----->在目标方法执行后增强,业务方法如果抛出异常,不执行通知public void afterReturning(){System.out.println("我是后置通知,出现异常不执行!");}//后置通知---->在目标方法执行后增强,业务如果抛出异常,仍执行通知public void after(){System.out.println("我是后置通知,出现异常仍然执行!");}//异常通知---->目标方法执行出现异常,才会执行通知public void afterThrowing(){System.out.println("我是异常通知");}}
配置织入
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"><!-- 注册目标对象 --><bean name="userService" class="com.haha.service.UserServiceImpl"></bean><!-- 注册通知对象 --><bean name="myAdvice" class="com.haha.advice.MyAdvice"></bean><!--aspectJ切点表达式public void com.haha.service.UserServiceImpl.save()* com.haha.service.*ServiceImpl.*(..)  --><!-- 配置注入 --><aop:config><!--指定切点  --><aop:pointcut expression="execution(* com.haha.service.*ServiceImpl.*(..))" id="myPC"/><!--指定通知==>切面ref:指定那个对象是通知对象  -->  <aop:aspect ref="myAdvice">  <!--指定myAdvice对象中的before方法织入到myPC(切点)指定的方法上  -->  <aop:around method="around" pointcut-ref="myPC"/>  </aop:aspect></aop:config></beans>
测试
import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.haha.service.UserService;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class test03 {@Resource(name="userService")private UserService us;@Testpublic void fun(){us.save();}}
注解版
在通知中写注解
import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;@Aspectpublic class MyAdvice2 {//配置切点@Pointcut("execution(* com.haha.service.*ServiceImpl.*(..))")public void myPC(){};//切面配置//MyAdvice2.myPC()=>引用该方法上的切点表达式@Before("MyAdvice2.myPC()")public void before(){System.out.println("我是前置通知");}//环绕通知---->在目标方法之前和之后都增强@Around("MyAdvice2.myPC()")public Object around(ProceedingJoinPoint pjp) throws Throwable{System.out.println("我是环绕通知,前半部分");//执行业务方法Object proceed=pjp.proceed();System.out.println("我是环绕通知,后半部分");return proceed;}//后置通知----->在目标方法执行后增强,业务方法如果抛出异常,不执行通知@AfterReturning("MyAdvice2.myPC()")public void afterReturning(){System.out.println("我是后置通知,出现异常不执行!");}//后置通知---->在目标方法执行后增强,业务如果抛出异常,仍执行通知@After("MyAdvice2.myPC()")public void after(){System.out.println("我是后置通知,出现异常仍然执行!");}//异常通知---->目标方法执行出现异常,才会执行通知@AfterThrowing("MyAdvice2.myPC()")public void afterThrowing(){System.out.println("我是异常通知");}}
配置织入
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"><!-- 注册目标对象 --><bean name="userService" class="com.haha.service.UserServiceImpl"></bean><!-- 注册通知对象 --><bean name="myAdvice" class="com.haha.advice.MyAdvice2"></bean><!-- 打开注解配置aop开关 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
测试
import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.haha.service.UserService;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext2.xml")public class test4 {@Resource(name="userService")private UserService us;@Testpublic void fun(){us.save();}}
你们可能会发现在我的代码中spring-aop好像使用的是jdk的动态代理的方式,代理对象为接口,其实在spring-aop中,它采用了动态代理和cglib两种方式,也就是如果配置切点的类有接口,那么就必须使用这个接口为被代理对象,spring的底层会自动去选择动态代理的方式,如果配置切点的类没有接口,那么就可以用自己作为被代理的类,spring的底层会自动的去选择cglib的方式去实现。