AOP,面向切面编程,就是当你已经完成,或者更加注意一些核心功能的时候。需要使用的编程过程,这个使用,你会发现,一个核心的业务,是由一个核心功能,和若干个额外服务类型的功能,统一组成的,比如数据验证,再比如日志的记录。
其实在场景中,我们的核心功能是针对数据库的操作,可能是增删过程。而服务类功能,比如记录日志,或者输入内容的验证过程。其实不是非有不可。没有这个功能,核心功能也能正常执行。
这个时候,我们就要考虑一个问题,这些必要或者非必要的额外功能,是不是要写在你的核心代码过程当中啊?
我们现在,将核心业务,做一个简単的切分
核心功能
辅助功能
1、明确名词
切点(pointcut) 在某些个核心方法之前,之后等位置
切点表达式:pointcut = execution(* com.lanou.test.service. * . * (…))execution(返回值 全包名.类名.方法名 (参数集合))增强(advisor) 辅助功能
织入(aspect) 将辅助功能,安装在切点上的过程
2、AOP增强的五种类型:
前置增强:before后置增强:after环绕增强:around异常增强:throw返回增强:after returning1、前置增强:
通过实现 MethodBeforeAdvice接口,然后编写前置通知的内容。
before方法参数的含义:
Method method:要被增强的方法Object[] objects:切点方法的参数Object o:要代理的类的对象(就是要执行方法的对象)所以前置通知可以拿到参数,并且修改参数或对参数做一些判断
package com.lanou.test.advice; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; public class MyBeforAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { // 如果传入的参数不是shiruntao的话,就不执行切点方法 if (objects[0].equals("shiruntao")) { System.out.println("我是AOP "); } else { throw new Exception("参数不是shiruntao"); } } }2、后置增强:
通过实现 AfterReturningAdvice, AfterAdvice 接口,然后编写后置通知的内容。
afterReturning方法参数的含义:
Object o:返回值对象
Method method:要被增强的方法
Object[] objects:切点方法的参数
Object o1:要代理的类的对象(就是要执行方法的对象)
所以后置通知可以拿到返回值,并且可以修改返回值
package com.lanou.test.advice; import org.springframework.aop.AfterAdvice; import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; public class MyAfterAdvice implements AfterReturningAdvice, AfterAdvice { @Override public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("你好,我是返回后置通知"); } }3、配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="service" class="com.lanou.test.service.LoginService"></bean> <bean id="myBeforeAdvice" class="com.lanou.test.advice.MyBeforeAdvice"></bean> <bean id="myAfterAdvice" class="com.lanou.test.advice.MyAfterAdvice"></bean> <aop:config> <!-- 切点设置 - execution(返回值 全包名.类名.方法名 (参数集合))--> <aop:pointcut id="methodsBefore" expression="execution(* com.lanou.test.service.*.*(..))"/> <!-- 给切点上设置前置增强--> <aop:advisor advice-ref="myBeforeAdvice" pointcut-ref="methodsBefore"></aop:advisor> <!-- 给切点上设置后置增强--> <aop:advisor advice-ref="myAfterAdvice" pointcut="execution(* com.lanou.test.service.*.*(..))"></aop:advisor> </aop:config> </beans>4、测试用例:
package com.lanou.test.advice; import org.springframework.aop.AfterAdvice; import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; public class MyAfterAdvice implements AfterReturningAdvice, AfterAdvice { @Override public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("你好,我是返回后置通知"); } } package com.lanou.test.advice; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; public class MyBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("Boss 好帅"); } }5、执行结果:
Boss 好帅 this is my logic 你好,我是返回后置通知 Boss 好帅 我是haha方法hajja 你好,我是返回后置通知创建描述增强的类:
package com.lanou.test.advicor; public class MyAdvicor { //前置通知 public void before(JoinPoint joinPoint) { System.out.println("before"); System.out.println(joinPoint.getArgs().length); //参数长度是1 System.out.println(joinPoint.getSignature().getName()); //方法的名字 System.out.println(joinPoint.getTarget()); //要被代理的对象 目标类对象 System.out.println(joinPoint.getThis()); //执行代理目标的对象 代理类对象 } //后置通知 public void after(JoinPoint joinPoint) { System.out.println("after"); } //后置返回通知 public void afterReturning(JoinPoint joinPoint) { System.out.println("afterReturning"); } //异常通知 public void afterThrowing(JoinPoint joinPoint) { System.out.println("afterThrowing"); } }配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="service" class="com.lanou.test.service.LoginService"></bean> <bean id="myAdvisor" class="com.lanou.test.advicor.MyAdvicor"></bean> <aop:config> <!-- 切点设置 - execution(返回值 全包名.类名.方法名 (参数集合))--> <aop:pointcut id="test" expression="execution(* com.lanou.test.service.*.*(..))"/> <aop:aspect ref="myAdvisor"> <aop:before method="before" pointcut-ref="test"></aop:before> <aop:after method="after" pointcut-ref="test"></aop:after> <aop:after-returning method="afterReturning" pointcut-ref="test"></aop:after-returning> <aop:after-throwing method="afterReturning" pointcut-ref="test"></aop:after-throwing> <!-- <aop:around method="around" pointcut-ref="test"></aop:around>--> </aop:aspect> </aop:config>执行结果:
before 我是lodic方法 after afterReturningaround通知可以修改参数也可以修改返回值。
创建描述增强的类:
import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; @Aspect @Configuration / @Component public class MyAdvicor { @Before("execution(* com.lanou.test.service.*.*(..))") public void before(JoinPoint joinPoint) { System.out.println("before"); System.out.println(joinPoint.getArgs().length); //参数长度是1 System.out.println(joinPoint.getSignature().getName()); //方法的名字 System.out.println(joinPoint.getTarget()); //要被代理的对象 System.out.println(joinPoint.getThis()); //执行代理目标的对象 } @After("execution(* com.lanou.test.service.*.*(..))") public void after(JoinPoint joinPoint) { System.out.println("after"); } @AfterReturning("execution(* com.lanou.test.service.*.*(..))") public void afterReturning(JoinPoint joinPoint) { System.out.println("afterReturning"); } @AfterThrowing("execution(* com.lanou.test.service.*.*(..))") public void afterThrowing(JoinPoint joinPoint) { System.out.println("afterThrowing"); } @Around("execution(* com.lanou.test.service.*.*(..))") public void around(JoinPoint joinPoint) { System.out.println("around"); } } 配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.lanou.test"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>jdk动态代理:生成代理对象快,只能根据接口生成
CGLIB动态代理:执行快,
xml中开启CGLIB动态代理 <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>