切面

作者: 陈俊亙 | 来源:发表于2018-01-20 16:07 被阅读0次
  
import org.aspectj.lang.JoinPoint;  
import org.aspectj.lang.ProceedingJoinPoint;  
import org.aspectj.lang.annotation.*;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
import java.util.List;  

@Aspect  
@Component  
public class ICacheAopAction {  
    @Pointcut("@annotation(com.cdms.aop.ICache)")  
    private void controllerAspect(){}  
  
    @Before("controllerAspect()")  
    public void Before(JoinPoint joinPoint){  
        String classname = joinPoint.getTarget().getClass().getSimpleName();  
        String methodName = joinPoint.getSignature().getName();  
        List<Object> args = Arrays.asList(joinPoint.getArgs());  
        System.out.println("@before Execute! --class name: " + classname + ", method name: " + methodName + " " + args );  
    }  
  
    @Around("controllerAspect()")  
    public Object Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {  
        System.out.println("@Around:执行目标方法之前...");  
        Object obj= proceedingJoinPoint.proceed();  
        System.out.println("@Around:执行目标方法之后...");  
        System.out.println("@Around:被织入的目标对象为:" + proceedingJoinPoint.getTarget());  
        System.out.println( "@Around:原返回值:" + obj + ",这是返回结果的后缀");  
        return obj;  
    }  
  
    @AfterThrowing("controllerAspect()")  
    public void AfterThrowing(){  
        System.out.println("异常通知....");  
    }  
  
    @After("controllerAspect()")  
    public void After(JoinPoint point){  
        System.out.println("@After:模拟释放资源...");  
        System.out.println("@After:目标方法为:" +  
                point.getSignature().getDeclaringTypeName() +  
                "." + point.getSignature().getName());  
        System.out.println("@After:参数为:" + Arrays.toString(point.getArgs()));  
        System.out.println("@After:被织入的目标对象为:" + point.getTarget());  
    }  
  
    @AfterReturning("controllerAspect()")  
    public void AfterReturning(JoinPoint point){  
        System.out.println("@AfterReturning:模拟日志记录功能...");  
        System.out.println("@AfterReturning:目标方法为:" +  
                point.getSignature().getDeclaringTypeName() +  
                "." + point.getSignature().getName());  
        System.out.println("@AfterReturning:参数为:" +  
                Arrays.toString(point.getArgs()));  
        System.out.println("@AfterReturning:返回值为:" );  
        System.out.println("@AfterReturning:被织入的目标对象为:" + point.getTarget());  
    }  
  
}```

相关文章

  • AOP

    关于面向切面编程的一些术语: 切面(Aspect):切面用于组织多个Advice,Advice放在切面中定义。 连...

  • AOP

    AOP切面编程 AOP: Aspect Oriented Programming 面向切面编程 面向切面编程(也叫...

  • 关于面向切面编程的一些术语:

    切面(Aspect):切面用于组织多个Advice,Advice放在切面中定义。 连接点(Joinpoint):程...

  • iOS - 切面编程 (Aspects解析)

    前言 首先我们了解几个概念,什么是切面编程?切面编程的实际应用? 切面编程(AOP):什么是切面?举个栗子:切一根...

  • 来自于宏的黑魔法 --- 简单实现面向切面编程

    Elixir Macro AOP 面向切面 宏 元编程 什么是 AOP (面向切面编程)? 面向切面编程 AOP(...

  • aop面向切面编程之AspectJ的简单应用

    面向切面 面向切面AOP(Aspect Oriented Programming),即面向切面编程, 是一种面向切...

  • aop之@Around

    1、切面类加上注解 2、给需要增强的方法添加切面方式一:在切面类中使用execution给批量方法添加切面 注:e...

  • MethodValidationInterceptor执行优先级

    一. 我遇到了什么问题? 如何让数据验证切面, 在缓存切面之后, 锁切面或事务切面之前执行? 背景: 我做了一个开...

  • 切面

  • 切面

    什么是AOP? AOP(Aspect Oriented Programming) 即面向切面编程,是OOP(Obj...

网友评论

      本文标题:切面

      本文链接:https://www.haomeiwen.com/subject/xhytaxtx.html