• spring 使用自定义注解


    1.  自定义注解

    2. aop 实现注解

    一、自定义注解

    @Target({ElementType.TYPE,ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    public @interface MyLog {
    
    }

    自定义注解需要使用元注解,即专门为自定义注解的注解 

    1. @Target
    2. @Rentention
    3. @Documented
    4. @Inherited

    @Target

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Target {
        /**
         * Returns an array of the kinds of elements an annotation type
         * can be applied to.
         * @return an array of the kinds of elements an annotation type
         * can be applied to
         */
        ElementType[] value();
    }

    需要传递 ElementType 类型的参数数组,指定该主节的作用域

    public enum ElementType {
        /** Class, interface (including annotation type), or enum declaration */
        TYPE,
    
        /** Field declaration (includes enum constants) */
        FIELD,
    
        /** Method declaration */
        METHOD,
    
        /** Formal parameter declaration */
        PARAMETER,
    
        /** Constructor declaration */
        CONSTRUCTOR,
    
        /** Local variable declaration */
        LOCAL_VARIABLE,
    
        /** Annotation type declaration */
        ANNOTATION_TYPE,
    
        /** Package declaration */
        PACKAGE,
    
        /**
         * Type parameter declaration
         *
         * @since 1.8
         */
        TYPE_PARAMETER,
    
        /**
         * Use of a type
         *
         * @since 1.8
         */
        TYPE_USE
    }

    @Retention

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Retention {
        /**
         * Returns the retention policy.
         * @return the retention policy
         */
        RetentionPolicy value();
    }

    @Retention 注解作用是指定主键的生存时间,SOURCE 、CLASS 、 RUNTIME

    public enum RetentionPolicy {
        /**
         * Annotations are to be discarded by the compiler.
         */
        SOURCE,
    
        /**
         * Annotations are to be recorded in the class file by the compiler
         * but need not be retained by the VM at run time.  This is the default
         * behavior.
         */
        CLASS,
    
        /**
         * Annotations are to be recorded in the class file by the compiler and
         * retained by the VM at run time, so they may be read reflectively.
         *
         * @see java.lang.reflect.AnnotatedElement
         */
        RUNTIME
    }

    @Doucmentd

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Documented {
    }

    无需传递参数,指定主键是否能够被Java 文档识别

    @Inherited

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Inherited {
    }

    无需传递参数,主键能够被继承,如果指定的请求的类型不存在,那么将会自动的寻找父类中的类型

    二、AOP 实现注解

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    Aop 面向切面编程

    @Aspect
    @Component
    public class MyLogAspect {
    
        private final Logger logger = LoggerFactory.getLogger(MyLogAspect.class);
    
        @Pointcut("@annotation(com.example.demo.annotation.MyLog)")
        public void pointCut(){}
    
        @Around("pointCut()")
        public void around(ProceedingJoinPoint joinPoint) throws Throwable{
            logger.info("around 前方法");
            joinPoint.proceed();
             logger.info("around 后方法");
        }
    
        @Before("pointCut()")
        public void doBefore(){
             logger.info("doBefore 方法");
        }
    
        @AfterReturning("pointCut()")
        public void doAfterReturn(JoinPoint joinPoint){
             logger.info("doAfterReturn 方法");
        }
    
        @After("pointCut()")
        public void doAfter(){
             logger.info("doAfter 方法");
        }
    
        @AfterThrowing("pointCut()")
        public void doThrowable(){
             logger.info("doThrowable 方法");
        }
    
    }
  • 相关阅读:
    Socket 之 同步以及异步通信
    Socket 之 c#实现Socket网络编程
    Socket 之 API函数介绍
    Socket 之 原理与编程基础
    C# 之 user32函数库
    WinServer 之 访问同网段服务器 或 同一服务器多虚拟机间的访问
    annex-b格式
    FLV文件格式解析
    PHP5中的stdClass
    web服务器【apache/nginx] 关闭目录的浏览权限
  • 原文地址:https://www.cnblogs.com/bytecodebuffer/p/13957049.html
Copyright © 2020-2023  润新知