Aop归纳

  • aop是动态代理的一种应用。对于有接口的类,使用原生的代理类,利用反射获取到类加载器,然后构造出构造函数,通过构造函数返一个代理对象,另一种没有实现接口的类使用的就是cglib代理,代理类继承自原类。
  • 使用:(aspect + component)
    pointcut 切点 (进行切点匹配)
    advice 通知 (对匹配到的进行织入) joinpoint 连接点|proceedingjoinpoint 进行连接点 ≈ target weaving 织入
@Aspect
@Slf4j
@Component
public class WebLogAspect {
    
    

    @Pointcut("execution( * com.gxdc.companymanage.controller..*(..))")
    public void webLog() {
    
    

    }

    @Around("webLog()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
    
    
        log.info("方法执行:" + joinPoint.getSignature().getDeclaringType().getName() + joinPoint.getSignature().getName());
        Object proceed = joinPoint.proceed();
        log.info("方法:" + joinPoint.getSignature().getName() + "执行完毕");
        return proceed;
    }

    @AfterThrowing(pointcut = "webLog()", throwing = "e")
    public void afterThrowingMehtod(JoinPoint joinPoint, Throwable e) {
    
    
        log.error("方法异常:" + joinPoint.getSignature().getDeclaringType().getName() + joinPoint.getSignature().getName() + "异常");
        log.error("异常信息:" + e.getMessage());
    }

}

猜你喜欢

转载自blog.csdn.net/howeres/article/details/111034837
AOP