zoukankan      html  css  js  c++  java
  • 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 方法");
        }
    
    }
  • 相关阅读:
    jsp转向
    什么是 XDoclet?
    tomcat中的几点配置说明
    mysql5问题
    POJ 3734 Blocks
    POJ 2409 Let it Bead
    HDU 1171 Big Event in HDU
    POJ 3046 Ant Counting
    HDU 2082 找单词
    POJ 1286 Necklace of Beads
  • 原文地址:https://www.cnblogs.com/bytecodebuffer/p/13957049.html
Copyright © 2011-2022 走看看