@Around的作用
注解方式:如果需要对某一方法进行增强,只需要在相应的方法上添加上自定义注解即可 package com.rq.aop.common.advice; import com.rq.aop.common.annotation.MyAnnotation; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect //标注增强处理类(切面类) @Component //交由Spring容器管理 public class AnnotationAspect { /* 可自定义切点位置,针对不同切点,方法上的@Around()可以这样写ex:@Around(value = "methodPointcut() && args(..)") @Pointcut(value = "@annotation(com.rq.aop.common.annotation.MyAnnotation)") public void methodPointcut(){} @Pointcut(value = "@annotation(com.rq.aop.common.annotation.MyAnnotation2)") public void methodPointcut2(){} */ //定义增强,pointcut连接点使用@annotation(xxx)进行定义 @Around(value = "@annotation(around)") //around 与 下面参数名around对应 public void processAuthority(ProceedingJoinPoint point,MyAnnotation around) throws Throwable{ System.out.println("ANNOTATION welcome"); System.out.println("ANNOTATION 调用方法:"+ around.methodName()); System.out.println("ANNOTATION 调用类:" + point.getSignature().getDeclaringTypeName()); System.out.println("ANNOTATION 调用类名" + point.getSignature().getDeclaringType().getSimpleName()); point.proceed(); //调用目标方法 System.out.println("ANNOTATION login success"); } } 注解类 package com.rq.aop.common.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)//运行时有效 @Target(ElementType.METHOD)//作用于方法 public @interface MyAnnotation { String methodName () default ""; } Controller package com.rq.aop.controller; import com.rq.aop.common.annotation.MyAnnotation; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/hello") public class HelloController { @RequestMapping("/login/{name}") @MyAnnotation(methodName = "login") public void login(@PathVariable String name){ System.out.println("hello!"+name); } } 运行结果: 匹配方法执行连接点方式 package com.rq.aop.common.advice; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Aspect @Component @Order(0) //设置优先级,值越低优先级越高 public class ExecutionAspect { @Around(value = "execution(* com.rq.aop.controller..*.*(..))") public void processAuthority (ProceedingJoinPoint point)throws Throwable{ System.out.println("EXECUTION welcome"); System.out.println("EXECUTION 调用方法:" + point.getSignature().getName()); System.out.println("EXECUTION 目标对象:" + point.getTarget()); System.out.println("EXECUTION 首个参数:" + point.getArgs()[0]); point.proceed(); System.out.println("EXECUTION success"); } } eg.
第一个表示匹配任意的方法返回值, …(两个点)表示零个或多个,第一个…表示service包及其子包,第二个表示所有类, 第三个*表示所有方法,第二个…表示方法的任意参数个数
|
|
来自: liang1234_ > 《ioc与aop》