Java annotation

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);
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容