Spring的两大特征,IOC和AOP相信做过java的都应该知道,但有几个人能把AOP说清楚呢?
最近在开发一个缓存组件,需要用到aop,但由于是工具项目,不能像业务开发一样使用@Aspect 和 xml配置的方式,这时才发现自己对aop的理解只是使用级。
所以自己又重新整理学习了AOP,受益良多,将学习的内容整理总结,方便以后自己复习也分享给需要的小伙伴。
(只是个人总结,如有错误欢迎指正)
首先分享一个自己整理的学习思维导图,可以从整体先了解下AOP的知识体系。
一、面向切面编程(AOP)基本概念
1. 什么是AOP
AOP(Aspect Oriented Programming)称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等。AOP是OOP的补充,解决了很多OOP不方便处理的功能,OOP是纵向的,AOP是横向的。
2. AOP基本概念
- 切面(Aspect) :官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”。
- 连接点(Joinpoint) :程序执行过程中的某一行为。
- 通知(Advice) :“切面”对于某个“连接点”所产生的动作。
- 切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。
- 目标对象(Target Object) :被一个或者多个切面所通知的对象。
- AOP代理(AOP Proxy) 在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。
3. 通知(Advice)类型
- 前置通知(Before advice) :在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。ApplicationContext中在aop:aspect里面使用aop:before元素进行声明。
- 后置通知(After advice) :当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。ApplicationContext中在aop:aspect里面使用aop:after元素进行声明。
- 返回后通知(After return advice) :在某连接点正常完成后执行的通知,不包括抛出异常的情况。ApplicationContext中在aop:aspect里面使用元素进行声明。
- 环绕通知(Around advice) :包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。ApplicationContext中在aop:aspect里面使用aop:around元素进行声明。
- 抛出异常后通知(After throwing advice) : 在方法抛出异常退出时执行的通知。 ApplicationContext中在aop:aspect里面使用aop:after-throwing元素进行声明。
4. AOP联盟标准
我们都是处于层次3,是基于层次2进行切面编程开发。
二、AOP使用实践
准备工作——jar包引入
使用aop除了Spring提供给开发者的jar包外,还需要两个jar包
- aopalliance.jar
- aspectjweaver.jar
1.基于经典代理的AOP开发
基于spring的aop的jar包,进行切面编码开发, 通过spring的bean配置完成面向切面的编程。
适用场景: 此种方式只依赖于spring的aop的jar包,适合与纯spring开发的项目场景,无需引入aspectj等外部服务包。
package com.spring.aop;
/**
* 谈恋爱接口
*
* @author Administrator
*
*/
public interface Love
{
/*
* 谈恋爱方法
*/
void fallInLove();
}
package com.spring.aop;
/**
* 人对象
* @author luwenbin006@163.com
*
*/
public class Person implements Love
{
/*
* 重写谈恋爱方法
* @see com.spring.aop.Love#fallInLove()
*/
public void fallInLove()
{
System.out.println("谈恋爱了...");
}
}
package com.spring.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
/**
* aop通知类
* @author luwenbin006@163.com
*
*/
public class LoveHelper implements MethodBeforeAdvice, AfterReturningAdvice
{
//调用方法之前执行
public void before(Method mtd, Object[] arg1, Object arg2) throws Throwable
{
System.out.println("谈恋爱之前必须要彼此了解!");
}
//调用方法之后执行
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable
{
System.out.println("我们已经谈了5年了,最终还是分手了!");
// System.out.println("我们已经谈了5年了,最终步入了结婚的殿堂!");
}
}
Spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 配置person对象 -->
<bean id="person" class="com.spring.aop.Person">
</bean>
<!-- 配置通知方法类 -->
<bean id="loveHelper" class="com.spring.aop.LoveHelper">
</bean>
<!-- 配置接口方法通知 也就是符合哪个条件的方法才进行通知 -->
<bean id="lovePointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*fallInLove" />
</bean>
<!-- 声明advisor 指定接口和通知类 -->
<bean id="loveHelperAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="loveHelper" />
<property name="pointcut" ref="lovePointcut" />
</bean>
<!-- 配置代理工厂 -->
<bean id="loveProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="person" />
<property name="interceptorNames" value="loveHelperAdvisor" />
<property name="proxyInterfaces" value="com.spring.aop.Love" />
</bean>
</beans>
2.@AspectJ注解驱动的AOP开发
实现@AspectJ注解开发,需要两步:
① 在类上使用 @Component 注解 把切面类加入到IOC容器中
② 在类上使用 @Aspect 注解 使之成为切面类
注意:在完成了引入AOP依赖包后,一般来说并不需要去做其他配置。
使用过Spring注解配置方式的人会问是否需要在程序主类中增加@EnableAspectJAutoProxy来启用,实际并不需要。
因为在AOP的默认配置属性中,spring.aop.auto属性默认是开启的,也就是说只要引入了AOP依赖后,默认已经增加了@EnableAspectJAutoProxy。
适用场景:AspectJ注解开发比较简单,建议业务系统开发需要自己定义切面编程,适用此方式。由于注解方式的编写方式特殊性,不适合与工具类的项目。
@Aspect
@Component
@Slf4j
public class WebLogAcpect {
/**
* 定义切入点
*/
@Pointcut("execution(public * com.gavin.example.aop..*.*(..))")
public void testPointcut(){}
/**
* 切入点前置处理:打印日志
*
* @param joinPoint
* @throws Throwable
*/
@Before("testPointcut()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 1.接收到请求,解析参数
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 2.记录下请求内容
log.info("URL : " + request.getRequestURL().toString());
}
@AfterReturning(returning = "result",pointcut = "testPointcut()")
public void doAfter(Object result) throws Throwable {
// 处理完请求,返回内容
log.info("RESPONSE : " + result);
}
}
3.注入式Aspect切面开发
Aspect配置aop开发分为2个部分 ,首先就是编写开发切面类,然后配置spring的配置文件,定义切面信息;
(1)Pointcut定义表达式
开发切面类的时候需要用到表达式定义Pointcut,表达式分为5个部分
-
execution(): 表达式主体。
-
第一个*号:表示返回类型,*号表示所有的类型。
-
包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,com.sample.service.impl包、子孙包下所有类的方法。
-
第二个*号:表示类名,*号表示所有的类。
-
*(…):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。
适用场景: 此种方式其实和方式二相同,只是一个是注解的开发方式 一个是配置的开发方式。个人建议尽量适用注解的方式开发,毕竟配置文件维护的开发维护难度远高于注解。所以此种方式适用于版本较老的项目(项目采用的是xml配置)
服务类
package com.spring.service.impl;
import com.spring.service.IUserManagerService;
public class UserManagerServiceImpl implements IUserManagerService{
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public String findUser(){
System.out.println("============执行业务方法findUser,查找的用户是:"+name+"=============");
return name;
}
public void addUser(){
System.out.println("============执行业务方法addUser=============");
//throw new RuntimeException();
}
}
切面类
package com.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class AopAspect {
/**
* 前置通知:目标方法调用之前执行的代码
* @param jp
*/
public void doBefore(JoinPoint jp){
System.out.println("===========执行前置通知============");
}
/**
* 后置返回通知:目标方法正常结束后执行的代码
* 返回通知是可以访问到目标方法的返回值的
* @param jp
* @param result
*/
public void doAfterReturning(JoinPoint jp,String result){
System.out.println("===========执行后置通知============");
System.out.println("返回值result==================="+result);
}
/**
* 最终通知:目标方法调用之后执行的代码(无论目标方法是否出现异常均执行)
* 因为方法可能会出现异常,所以不能返回方法的返回值
* @param jp
*/
public void doAfter(JoinPoint jp){
System.out.println("===========执行最终通知============");
}
/**
*
* 异常通知:目标方法抛出异常时执行的代码
* 可以访问到异常对象
* @param jp
* @param ex
*/
public void doAfterThrowing(JoinPoint jp,Exception ex){
System.out.println("===========执行异常通知============");
}
/**
* 环绕通知:目标方法调用前后执行的代码,可以在方法调用前后完成自定义的行为。
* 包围一个连接点(join point)的通知。它会在切入点方法执行前执行同时方法结束也会执行对应的部分。
* 主要是调用proceed()方法来执行切入点方法,来作为环绕通知前后方法的分水岭。
*
* 环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法。
* 而且环绕通知必须有返回值,返回值即为目标方法的返回值
* @param pjp
* @return
* @throws Throwable
*/
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("======执行环绕通知开始=========");
// 调用方法的参数
Object[] args = pjp.getArgs();
// 调用的方法名
String method = pjp.getSignature().getName();
// 获取目标对象
Object target = pjp.getTarget();
// 执行完方法的返回值
// 调用proceed()方法,就会触发切入点方法执行
Object result=pjp.proceed();
System.out.println("输出,方法名:" + method + ";目标对象:" + target + ";返回值:" + result);
System.out.println("======执行环绕通知结束=========");
return result;
}
}
Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 声明一个业务类 -->
<bean id="userManager" class="com.spring.service.impl.UserManagerServiceImpl">
<property name="name" value="lixiaoxi"></property>
</bean>
<!-- 声明通知类 -->
<bean id="aspectBean" class="com.spring.aop.AopAspect" />
<aop:config>
<aop:aspect ref="aspectBean">
<aop:pointcut id="pointcut" expression="execution(* com.spring.service.impl.UserManagerServiceImpl..*(..))"/>
<aop:before method="doBefore" pointcut-ref="pointcut"/>
<aop:after-returning method="doAfterReturning" pointcut-ref="pointcut" returning="result"/>
<aop:after method="doAfter" pointcut-ref="pointcut" />
<aop:around method="doAround" pointcut-ref="pointcut"/>
<aop:after-throwing method="doAfterThrowing" pointcut-ref="pointcut" throwing="ex"/>
</aop:aspect>
</aop:config>
</beans>
4.基于AspectJ配置的纯POJO切面开发
适用场景: 此方式是我最终开发缓存插件的时候采用的方式,适合工具类的项目使用,可以通过springboot的starter模式完成,切面编程相关的功能引入生效,最大化的减少使用成本。
此方式通过aspectj的api进行代码切面开发,通过springboot的@Bean配置,完成Advisor的注入生效,样例待补充。
相关参考资料:
https://blog.csdn.net/wyl6019/article/details/80136000
https://www.cnblogs.com/xyhero/p/60829d413d54b280ff2f143be19029b4.html
https://www.cnblogs.com/pwc1996/p/4839150.html
https://blog.csdn.net/lmb55/article/details/82470388