美文网首页
Java annotation

Java annotation

作者: 一颗北上广的心 | 来源:发表于2017-11-16 11:48 被阅读0次
    package com.citi.yf.annotation;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target({ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.LOCAL_VARIABLE,ElementType.METHOD,ElementType.PARAMETER,ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {
        
        public String name();
    
    }
    
    
    package com.citi.yf.annotation;
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    
    public class AnnotationProcessor {
    
        public static void process(Annotation... annotations){
            for (Annotation annotation : annotations) {
                if (annotation instanceof MyAnnotation) {
                    System.out.println(((MyAnnotation) annotation).name());
                }
            }
        }
        
     
        public static void main(String[] args) {
    
            Entity entity = new Entity();
            //type
            process(Entity.class.getAnnotations());
            //constructor
            for (Constructor constructor: entity.getClass().getConstructors()) {
                process(constructor.getAnnotations());
            }
            //field
            for (Field field : entity.getClass().getDeclaredFields()) {
                process(field.getAnnotations());
            }
            //method
            for (Method method : entity.getClass().getMethods()) {
                process(method.getAnnotations());
                //param
                for (Annotation[] annotations : method.getParameterAnnotations()) {
                    
                    process(annotations);
                }
            }
            
        }
        
    }
    
    

    使用切片,切如带有注解的方法,并根据注解执行下一步操作

    
    @Component
    @Aspect
    public class RedisControlAspectJ {
    
        final static Logger logger = LoggerFactory.getLogger(RedisControlAspectJ.class);
    
        static RedisUtils redisUtils = new RedisUtils();
        
        //切入点
        @Pointcut("execution(* com.impl.*.*(..))")
        public void doing() {
        }
        //符合切入点,且方法包含RedisDel注解
        @AfterReturning("doing() && @annotation(redisDel)")
        public void process(JoinPoint point, RedisDel redisDel) {
    
            for (RedisKeyEnum key : redisDel.redisKey()) {
                redisDeleteKey(key);
            }
    
        }
    
        void redisDeleteKey(RedisKeyEnum keyEnum) {
            String key = keyEnum.getName();
            redisUtils.delkeyObject(key);
    
            logger.info("delete redis ->{}", key);
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Java annotation

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