概念:
AOP,面向切面编程,就是当你已经完成,或者更加注意一些核心功能的时候。需要使用的编程过程,这个使用,你会发现,一个核心的业务,是由一个核心功能,和若干个额外服务类型的功能,统一组成的,比如数据验证,再比如日志的记录。
其实在场景中,我们的核心功能是针对数据库的操作,可能是增删过程。而服务类功能,比如记录日志,或者输入内容的验证过程。其实不是非有不可。没有这个功能,核心功能也能正常执行。
spring-AOP 的实现:
切点(pointcut) 在某些个核心方法之前,之后等位置
- 切点表达式:pointcut = execution(* com.lanou.test.service. * . * (…))
- execution(返回值 全包名.类名.方法名 (参数集合))
增强(advisor) 辅助功能
织入(aspect) 将辅助功能,安装在切点上的过程
AOP增强的五种类型:
- 前置增强:before
- 后置增强:after
- 环绕增强:around
- 异常增强:throw
- 返回增强:after returning
通过实现接口的方式实现增强
1、前置增强:
通过实现 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:要代理的类的对象(就是要执行方法的对象)
配置如下:
<?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>
测试用例:
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 好帅");
}
}
执行结果:
- Boss 好帅
- this is my logic
- 你好,我是返回后置通知
- Boss 好帅
- 我是haha方法hajja
- 你好,我是返回后置通知