zoukankan      html  css  js  c++  java
  • 基于AspectJ自定义注解

     1 package com.aspectj.demo.aspect;
     2 
     3 import java.lang.annotation.ElementType;
     4 import java.lang.annotation.Retention;
     5 import java.lang.annotation.RetentionPolicy;
     6 import java.lang.annotation.Target;
     7 
     8 @Retention(RetentionPolicy.RUNTIME)
     9 @Target(ElementType.METHOD)
    10 public @interface MonitorMethod {
    11     String value() default "";
    12 }
     1 package com.aspectj.demo.aspect;
     2 
     3 import java.lang.reflect.Method;
     4 
     5 import org.aspectj.lang.ProceedingJoinPoint;
     6 import org.aspectj.lang.annotation.Around;
     7 import org.aspectj.lang.annotation.Aspect;
     8 
     9 @Aspect
    10 public class MethodExecutionTime {
    11 
    12 //    @Around("execution(* *.*(..)) && @annotation(com.aspectj.demo.aspect.MonitorMethod)")
    13     @Around("execution(* *.*(..)) && @annotation(method)")
    14     public Object profile(ProceedingJoinPoint pjd,MonitorMethod method) throws Throwable {        
    15         Object result = null;
    16         System.out.println(method.value());
    17         // 这里可以获取方法名
    18         System.out.println(pjd.getSignature().getName());
    19         System.out.println(pjd.getTarget());
    20         // 获取方法名
    21         Method[] methods = pjd.getSignature().getDeclaringType().getMethods();
    22         System.out.println(methods[0]);
    23 
    24         // 获取参数信息
    25         Object[] args = pjd.getArgs();
    26         for (Object o : args) {
    27             System.out.println(o.toString());
    28         }
    29         try {
    30             // System.out.println("前置通知");
    31             result = pjd.proceed();
    32             // System.out.println("后置通知");
    33         } catch (Throwable e) {
    34             // System.out.println("异常通知");
    35         }
    36         // System.out.println("返回通知");
    37         return result;
    38     }
    39 }
     1 package com.aspectj.demo.aspect;
     2 
     3 public class StockService {
     4 
     5     @MonitorMethod(value="1111111111")
     6     public String getBaseInfo(String a) {
     7         try {
     8             Thread.sleep(500);
     9         } catch (InterruptedException e) {
    10             e.printStackTrace();
    11         }
    12         return "";
    13     }
    14 }

    有追求,才有动力!

    向每一个软件工程师致敬!

    by wujf

    mail:921252375@qq.com

  • 相关阅读:
    Binary Tree Zigzag Level Order Traversal
    Binary Tree Level Order Traversal
    Symmetric Tree
    Best Time to Buy and Sell Stock II
    Best Time to Buy and Sell Stock
    Triangle
    Populating Next Right Pointers in Each Node II
    Pascal's Triangle II
    Pascal's Triangle
    Populating Next Right Pointers in Each Node
  • 原文地址:https://www.cnblogs.com/wujf/p/5363209.html
Copyright © 2011-2022 走看看