• 自定义注解、数字字典注解


    目录

    一、基本注解

    二、自定义注解及使用

     三、注解日志


    一、基本注解

    @Retention:定义注解的保留策略
    @Retention(RetentionPolicy.SOURCE)             //注解仅存在于源码中,在class字节码文件中不包含
    @Retention(RetentionPolicy.CLASS)              //默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
    @Retention(RetentionPolicy.RUNTIME)            //注解会在class字节码文件中存在,在运行时可以通过反射获取到

    @Target:指定被修饰的Annotation可以放置的位置(被修饰的目标)
    @Target(ElementType.TYPE)                      //接口、类
    @Target(ElementType.FIELD)                     //属性
    @Target(ElementType.METHOD)                    //方法
    @Target(ElementType.PARAMETER)                 //方法参数
    @Target(ElementType.CONSTRUCTOR)               //构造函数
    @Target(ElementType.LOCAL_VARIABLE)            //局部变量
    @Target(ElementType.ANNOTATION_TYPE)           //注解
    @Target(ElementType.PACKAGE)                   //包
    注:可以指定多个位置,例如:
    @Target({ElementType.METHOD, ElementType.TYPE}),也就是此注解可以在方法和类上面使用

    @Inherited:指定被修饰的Annotation将具有继承性

    @Documented:指定被修饰的该Annotation可以被javadoc工具提取成文档.

    注解分类(根据Annotation是否包含成员变量,可以把Annotation分为两类):

    标记Annotation:
    没有成员变量的Annotation; 这种Annotation仅利用自身的存在与否来提供信息

    元数据Annotation:
    包含成员变量的Annotation; 它们可以接受(和提供)更多的元数据;

    二、自定义注解及使用

     如我定义一个注解 myAnotaion1

    1. package com.zking.ssm.annotation;
    2. import java.lang.annotation.ElementType;
    3. import java.lang.annotation.Retention;
    4. import java.lang.annotation.RetentionPolicy;
    5. import java.lang.annotation.Target;
    6. /**
    7. * @author 锦鲤
    8. * @site www.lucy.com
    9. * @company xxx公司
    10. * @create  2022-10-27 11:12
    11. * 定义注解myAnotaion1
    12. */
    13. //类,方法,属性
    14. @Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
    15. @Retention(RetentionPolicy.RUNTIME)
    16. public @interface myAnotaion1 {
    17. public String value() default "value修饰类、方法、属性";//内容
    18. public String desc() default "desc修饰类、方法、属性";//内容
    19. }

     myAnotaion2

    1. package com.zking.ssm.annotation;
    2. import java.lang.annotation.ElementType;
    3. import java.lang.annotation.Retention;
    4. import java.lang.annotation.RetentionPolicy;
    5. import java.lang.annotation.Target;
    6. /**
    7. * @author 锦鲤
    8. * @site www.lucy.com
    9. * @company xxx公司
    10. * @create  2022-10-27 11:12
    11. * 定义注解myAnotaion1
    12. */
    13. //类,方法,属性
    14. @Target({ElementType.PARAMETER,ElementType.METHOD})
    15. @Retention(RetentionPolicy.RUNTIME)
    16. public @interface myAnotaion2 {
    17. public String value() default "value修饰类、方法、属性";//内容
    18. public String desc() default "desc修饰类、方法、属性";//内容
    19. }

     注解使用

    1. package com.zking.ssm.annotation;
    2. /**
    3. * @author 锦鲤
    4. * @site www.lucy.com
    5. * @company xxx公司
    6. * @create  2022-10-27 11:17
    7. */
    8. @myAnotaion1(value = "标记在类上面")
    9. public class StudentController {
    10. //标记注解
    11. @myAnotaion1( "标记在属性id上面")
    12. private String id;
    13. @myAnotaion1("标记在属性name上面")
    14. private String name;
    15. @myAnotaion2
    16. public void test(@myAnotaion2("用来修饰id参数") String id,@myAnotaion2("用来修饰name参数")String name){
    17. System.out.println("测试");
    18. }
    19. }

    测试

    1. package com.zking.ssm.demo;
    2. import com.zking.ssm.annotation.StudentController;
    3. import com.zking.ssm.annotation.myAnotaion1;
    4. /**
    5. * @author 锦鲤
    6. * @site www.lucy.com
    7. * @company xxx公司
    8. * @create  2022-10-27 11:48
    9. * 目标:
    10. * 1.获取 StudentController 类上的自定义注解中的内容
    11. *1.获取 StudentController 方法上的自定义注解中的内容
    12. * 1.获取 StudentController 属性上的自定义注解中的内容
    13. * 1.获取 StudentController 参数上的自定义注解中的内容
    14. */
    15. public class Demo3 {
    16. public static void main(String[] args) {
    17. 1.获取 StudentController 类上的自定义注解中的内容
    18. //获得自定义注解myAnotaion1
    19. myAnotaion1 anotaion=StudentController.class.getAnnotation(myAnotaion1.class);
    20. System.out.println(anotaion.value());
    21. System.out.println(anotaion.desc());
    22. }
    23. }

     

     报错原因:改为

    @Retention(RetentionPolicy.RUNTIME)


    //        2.获取 StudentController 属性上的自定义注解中的内容
    //     获取属性
           Field id= StudentController.class.getDeclaredField("id");
            Field name= StudentController.class.getDeclaredField("name");
    //      拿到内容
            System.out.println(id.getAnnotation(myAnotaion1.class).value());
            System.out.println(name.getAnnotation(myAnotaion1.class).value());


     

     //  3.获取 StudentController 方法上的自定义注解中的内容
    Method m1= StudentController.class.getDeclaredMethod("test", String.class, String.class);
     System.out.println(m1.getAnnotation(myAnotaion2.class).value());
    
    
    
    
    

     

    //        4.获取 StudentController 参数上的自定义注解中的内容
    //       获取方法
            Method m1= StudentController.class.getDeclaredMethod("test", String.class, String.class);
    //获取方法上的参数
          Parameter[] ps= m1.getParameters();
    
            for (Parameter parameter : ps) {
                System.out.println(parameter.getAnnotation(myAnotaion2.class).value());
            }
    

     


     三、注解日志

     定义注解:Mylog

    p定义ackage
     com.zking.ssm.Aop;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * @author 锦鲤
     * @site www.lucy.com
     * @company xxx公司
     * @create  2022-10-27 15:16
     */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyLog {
    
        String desc();
    
    }
    

     DemoController

    package com.zking.ssm.Aop;
    
    import com.zking.ssm.Aop.MyLog;
    import org.springframework.stereotype.Controller;
    
    /**
     * @author 锦鲤
     * @site www.lucy.com
     * @company xxx公司
     * @create  2022-10-27 15:14
     */
    @Controller
    public class DemoController {
    
        @MyLog(desc = "方法的内容")
        public  void test(){
            System.out.println("测试方法");
        }
    }
    

     MyLogAspect  日志文件

    package com.zking.ssm.Aop;
    
    import com.zking.ssm.Aop.MyLog;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    /**
     * @author 锦鲤
     * @site www.lucy.com
     */
    @Component
    @Aspect
    public class MyLogAspect {
        private static final Logger logger = LoggerFactory.getLogger(MyLogAspect.class);
    
        /**
         * 只要用到了com.javaxl.p2.annotation.springAop.MyLog这个注解的,就是目标类
         */
        @Pointcut("@annotation(com.zking.ssm.Aop.MyLog)")
        private void MyValid() {
        }
    
        @Before("MyValid()")
        public void before(JoinPoint joinPoint) {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            logger.debug("[" + signature.getName() + " : start.....]");
            System.out.println("[" + signature.getName() + " : start.....]");
    
            MyLog myLog = signature.getMethod().getAnnotation(MyLog.class);
            logger.debug("【目标对象方法被调用时候产生的日志,记录到日志表中】:"+myLog.desc());
            System.out.println("【目标对象方法被调用时候产生的日志,记录到日志表中】:" + myLog.desc());
        }
    }

     测试ClazzBizTest

    package com.zking.shiro;
    
    import com.zking.ssm.Aop.DemoController;
    import com.zking.ssm.biz.ClazzBiz;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    /**
     * @author小李飞刀
     * @site www.javaxl.com
     * @company xxx公司
     * @create  2022-10-26 15:29
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:applicationContext.xml"})
    public class ClazzBizTest {
      /*  @Autowired
        private ClazzBiz clazzBiz;
    */
        @Autowired
        private DemoController demoController;
    
    
    @Test
    //    demoController
       public  void test1(){
           demoController.test();//调用
       }
    
    
    }
    


  • 相关阅读:
    Linux:理解进程的多种状态
    小学生写作业用什么灯最好?分享学生专用的暖光LED灯
    芋道系统,springboot+vue3+mysql实现地址的存储与显示
    css3文字环绕旋转
    three.js入门 ---- 相机控件OrbitControls
    mysql查询:行转列与列转行
    统计软件与数据分析Lesson15----梯度下降(Gradient Descent)过程可视化
    QT 音乐播放器【二】 歌词同步+滚动+特效
    4. null 和 undefined区别?
    JVM基础知识(一)jvm内存结构
  • 原文地址:https://blog.csdn.net/qq_66924116/article/details/127551452