@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {

    String key();

} 

  1. Annotation型定义为 @interface, 所有的Annotation会自动继承java.lang.Annotation这一接口,并且不能再去继承别的类或是接口.
  2. 参数成员只能用public或默认(default)这两个访问权修饰
  3. 参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和String、Enum、Class、annotations等数据类型,以及这一些类型的数组.

@Documented 注解
功能:指明修饰的注解,可以被例如javadoc此类的工具文档化,只负责标记,没有成员取值。

@Retention 注解
功能:指明修饰的注解的生存周期,即会保留到哪个阶段。
RetentionPolicy的取值包含以下三种:
SOURCE:源码级别保留,编译后即丢弃。
CLASS:编译级别保留,编译后的class文件中存在,在jvm运行时丢弃,这是默认值。
RUNTIME: 运行级别保留,编译后的class文件中存在,在jvm运行时保留,可以被反射调用。

@Target 注解
功能:指明了修饰的这个注解的使用范围,即被描述的注解可以用在哪里。
ElementType的取值包含以下几种:

TYPE:类,接口或者枚举
FIELD:域,包含枚举常量
METHOD:方法
PARAMETER:参数
CONSTRUCTOR:构造方法
LOCAL_VARIABLE:局部变量
ANNOTATION_TYPE:注解类型
PACKAGE:包

@Pointcut("@annotation( com.ztesoft.zsmart.bss.ucc.cloud.annotation.MyAnnotation)")
    public void myPointCut() {
    }
 
@Aspect
@Component
public class MyInterceptor {
    @Pointcut("@annotation( com.ztesoft.zsmart.bss.ucc.cloud.annotation.MyAnnotation)")
    public void myPointCut() {
    }

    /**
     * 方法拦截
     *
     * @param pjp 拦截点
     * @return 执行结果
     * @throws Throwable 异常
     */
    @Around("myPointCut()")
    public Object doInterceptor(ProceedingJoinPoint pjp) throws BaseAppException {
        // logger.debug("doInterceptor begin..");
        Object result;
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        // 获取被拦截的方法
        Method method = signature.getMethod();
        MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);

        Class returnType = method.getReturnType();
        signature.getParameterTypes();
        String key = myAnnotation.key();

        result = this.getResult(key, returnType);

        if (result == null) {
            try {
                result = pjp.proceed();
            }
            catch (Throwable t) {
                logger.warn("pjp.proceed() error ", t);
            }
        }

        return result;
    }
    
}

private Object getResult(String key, Class returnType) {
       
        String value = this.localParamMap.get(key);
        Object result;
        if (returnType == Long.class || returnType == long.class) {
            result = NumberUtils.toLong(value.trim());
        }
        else if (returnType == Integer.class || returnType == int.class) {
            result = NumberUtils.toInt(value.trim());
        }
        else if (returnType == Boolean.class || returnType == boolean.class) {
            result = Boolean.valueOf(value.trim());
        }
        else {
            result = value.trim();
        }
        
        return result;
    }    

 

参考

https://blog.csdn.net/liucy007/article/details/123827715