关于aop切面 注解、参数如何获取

网友投稿 1952 2022-11-09

关于aop切面 注解、参数如何获取

关于aop切面 注解、参数如何获取

目录aop切面 注解、参数如何获取定义需要切面的注解在需要进行切面的方法标注注解定义切面aop中获取自定义注解的属性值自定义注解用在方法上获取注解的属性值

aop切面 注解、参数如何获取

在工作中会经常使用aop,这里将aop使用基本方法,获取在切点中使用的获取参数、注解做一个样例。

定义需要切面的注解

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface AnnDemo {

String value();

boolean isAop() default true;

}

在需要进行切面的方法标注注解

@RestController

@RequestMapping("/order")

public class OrderController {

@Autowired

private OrderService orderService;

@RequestMapping("/all")

@AnnDemo(value = "all",isAop = false)

public List findAll() {

List list = orderService.getOrderList();

return list;

}

@RequestMapping("/page")

@AnnDemo(value = "page")

public List findPage(@RequestParam("username") String username) {

List listPage = orderService.getOrdersListPage();

return listPage;

}

}

定义切面

在切面中获取切点注解,方法,参数的获取

@Aspect

@Component

public class AspectDemo {

@Pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..))")

public void excetionMethod() {}

@Pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(AnnDemo)")

public void excetionNote() { }

@Before("excetionMethod()")

public void testBefore(JoinPoint joinPoint) {

System.out.println("----------------------------前置通知---");

Object[] args = joinPoint.getArgs();

for (Object arg : args) {

System.out.println(arg);

}

}

@Around(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(AnnDemo)")

public Object testBeforeNote(ProceedingJoinPoint joinPoint) throws Throwable {

//用的最多通知的签名

Signature signature = joinPoint.getSignature();

MethodSignature msg=(MethodSignature) signature;

Object target = joinPoint.getTarget();

//获取注解标注的方法

Method method = target.getClass().getMethod(msg.getName(), msg.getParameterTypes());

//通过方法获取注解

AnnDemo annotation = method.getAnnotation(AnnDemo.class);

Object proceed;

//获取参数

Object[] args = joinPoint.getArgs();

System.out.println(annotation.value());

System.out.println(annotation.isAop());

for (Object arg : args) {

System.out.println(arg);

}

if (Objects.isNull(annotation) || !annotation.isAop()) {

System.out.println("无需处理");

proceed = joinPoint.proceed();

}else {

System.out.println("进入aop判断");

proceed = joinPoint.proceed();

if(proceed instanceof List){

List proceedLst = (List) proceed;

if(!CollectionUtils.isEmpty(proceedLst)){

TbOrder tbOrder = new TbOrder();

tbOrder.setPaymentType("fffffffffffffffffff");

ArrayList tbOrderLst = new ArrayList<>();

tbOrderLst.add(tbOrder);

return tbOrderLst;

}

}

System.out.println(proceed);

}

return proceed;

}

}

aop中获取自定义注解的属性值

自定义注解

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface SystemLog {

public String description() default "";

}

用在方法上

@ResponseBody

@ValidRequestBody

@RequestMapping("/login")

@SystemLog(description="登录")

public GlobalRespoDIXceuonse login(@RequestBody @Valid User user, BindingResult bindingResult){

......

}

获取注解的属性值

@Around("@annotation(com.xxx.xxx.xxx.SystemLog)")

public Object around(ProceedingJoinPoint joinPoint) throws Throwable{

SystemLog systemLog = ((MethodSignature)joinPoint.getSignature()).getMethod().getAnnotation(SystemLog.class);

......

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:华为-字串的连接最长路径查找
下一篇:华为-字符串分隔
相关文章

 发表评论

暂时没有评论,来抢沙发吧~