前面的博客中,简单的介绍了IOC,这篇博客将向大家介绍Aop,面向切面编程。
可能一说面向切面编程,不好理解。什么是切面?怎么还编程?什么鬼?大家以前可能听的多的是面向对象编程、面向接口编程、面向函数编程等等。
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
面向切面编程,就会有一个切面。切面包含了切入点和通知。切面可以理解成一个面,当这个面相关的方法发生运行的时候,就会触发联动的方法,也就是spring中的通知。
切面(Aspect) :通知和切入点共同组成了切面,时间、地点和要发生的“故事”。
通知(Advice) :通知定义了切面是什么以及何时使用。描述了切面要完成的工作和何时需要执行这个工作。
切入点(Pointcut) :通知定义了切面要发生的“故事”和时间,那么切入点就定义了“故事”发生的地点,例如某个类或方法的名称。
连接点(Joinpoint) :程序能够应用通知的一个“时机”,这些“时机”就是连接点,例如方法被调用时、异常被抛出时等等。
目标对象(Target Object) :即被通知的对象。
AOP代理(AOP Proxy) 在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理;反之,采用CGLIB代理。
execution函数用于匹配方法执行的连接点,语法为:
execution(方法修饰符(可选) 返回类型 方法名 参数 异常模式(可选))
参数部分允许使用通配符:
* 匹配任意字符,但只能匹配一个元素
.. 匹配任意字符,可以匹配任意多个元素,表示类时,必须和*联合使用
+ 必须跟在类名后面,如Horseman+,表示类本身和继承或扩展指定类的所有类
除了execution(),Spring中还支持其他多个函数,这里列出名称和简单介绍,以方便根据需要进行更详细的查询
@annotation()
表示标注了指定注解的目标类方法
例如 @annotation(org.springframework.transaction.annotation.Transactional) 表示标注了@Transactional的方法
args()
通过目标类方法的参数类型指定切点
例如 args(String) 表示有且仅有一个String型参数的方法
如 @args(org.springframework.stereotype.Service) 表示有且仅有一个标注了@Service的类参数的方法
with()
通过类名指定切点
如 with(examples.chap03.Horseman) 表示Horseman的所有方法
target()
通过类名指定,同时包含所有子类
如 target(examples.chap03.Horseman) 且Elephantman extends Horseman,则两个类的所有方法都匹配
@within()
匹配标注了指定注解的类及其所有子类
如 @within(org.springframework.stereotype.Service) 给Horseman加上@Service标注,则Horseman和Elephantman 的所有方法都匹配
@target()
所有标注了指定注解的类
如 @target(org.springframework.stereotype.Service) 表示所有标注了@Service的类的所有方法
this()
大部分时候和target()相同,区别是this是在运行时生成代理类后,才判断代理类与指定的对象类型是否匹配
表达式可由多个切点函数通过逻辑运算组成
&&
与操作,求交集,也可以写成and
例如 execution(* chop(..)) && target(Horseman) 表示Horseman及其子类的chop方法
||
或操作,求并集,也可以写成or
例如 execution(* chop(..)) || args(String) 表示名称为chop的方法或者有一个String型参数的方法
!
非操作,求反集,也可以写成not
例如 execution(* chop(..)) and !args(String) 表示名称为chop的方法但是不能是只有一个String型参数的方法
execution常用于匹配特定的方法,如update时怎么处理,或者匹配某些类,如所有的controller类,是一种范围较大的切面方式,多用于日志或者事务处理等。
其他的几个用法各有千秋,视情况而选择。
我们可以通过xml来配置,也可以使用注解来配置:
xml配置切点和通知:
使用注解配置:
package com.zero.aspect;
import com.zero.config.RequestAttributeConst;
import com.zero.web.RequestDetailsLogger;
import com.zero.web.ResponseDetailsLogger;
import com.zero.web.ServletContextHolder;
import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import java.lang.reflect.Method;
import java.time.OffsetDateTime;
/**
* 本类设计为当有被@RequestBodyLogs修饰的@ControllerAdvice或者@Controller抛出异常时记录输入输出,
* 其他情况仅记录被标记的@RequestMapping或@ResponseBody方法
*
* @author soul
* @see //RequestLogging
* @see org.springframework.web.bind.annotation.ControllerAdvice
*/
@Aspect
public class RequestLoggingAspect implements Ordered {
private static final Logger LOGGER = LoggerFactory.getLogger(RequestLoggingAspect.class);
private final int order;
public RequestLoggingAspect(int order) {
this.order = order;
}
//环绕通知,切点是同时满足 1.com.zero包下 2.带有@ResponseBody或@RequestMapping注解 3.带有自定义注解@RequestLogging 的方法
@Around(value = "within(com.zero..*) " +
"&& (@annotation(org.springframework.web.bind.annotation.ResponseBody)" +
"|| @annotation(org.springframework.web.bind.annotation.RequestMapping)) " +
"&& @annotation(com.zero.RequestLogging)")
public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
// 生成请求日志
RequestDetailsLogger requestLog = generateJsonRequestDetails();
// 获取Swagger上的API描述
injectApiOperationDescription(joinPoint, requestLog);
// 执行真实请求
final Object proceed = joinPoint.proceed();
// 当响应完成时, 打印完整的'request & response'信息
requestLog.setResponseTime(OffsetDateTime.now());
LOGGER.info("RequestLoggingAspect#\r\nREQUEST->\r\n{}\r\nRESPONSE->\r\n {}", requestLog, ResponseDetailsLogger.with(proceed));
// 放行
return proceed;
}
/**
* 创建通用的日志输出模式并绑定线程
*
* @return 日志模型
*/
private RequestDetailsLogger generateJsonRequestDetails() {
RequestDetailsLogger logDetails = (RequestDetailsLogger) ServletContextHolder.getRequest().getAttribute(RequestAttributeConst.DETAILS_KEY);
if (logDetails == null) {
logDetails = new RequestDetailsLogger();
ServletContextHolder.getRequest().setAttribute(RequestAttributeConst.DETAILS_KEY, logDetails);
}
return logDetails;
}
private void injectApiOperationDescription(ProceedingJoinPoint joinPoint, RequestDetailsLogger logDetails) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
final ApiOperation operate = method.getAnnotation(ApiOperation.class);
if (operate != null) {
logDetails.setApiDesc(operate.value());
}
}
@Override
public int getOrder() {
return order;
}
}
@ResponseBody
@RequestLogging
@ExceptionHandler(RestStatusException.class)
public Object restStatusException(Exception e, HttpServletRequest request) {
// 取出存储在Shift设定在Request Scope中的ErrorEntity
return request.getAttribute(e.getMessage());
}
可以说spring AOP是一种很有意思的编程思想,只有自己动手实践,多理解切面是如何触发的,小编现在也是理解不够深刻,对AOP中使用的动态代理也是不清楚,以后理解深刻了,再做进一步的总结。现在对切点、通知、连接点、切面有了了解。
springAOP 做日志拦截是非常棒的,下面的博客向大家带来springAop的日志拦截。