java自定义注解(Annotation)及自定义注解结合Aop数据字典应用

转载请标明出处:https://blog.csdn.net/men_ma/article/details/106847165.
本文出自 不怕报错 就怕不报错的小猿猿 的博客

背景

在平时开发过程中经常看到各种各样的注解,例如@Component、@Resource、@Override 等等,注解极大方便了我们开发,今天就了解下注解,看下我们如何自定义注解。自定义注解前 我们必须要了解元注解和相关定义注解的语法。

目标

1、Java注解简介
2、Java元注解(重点)
3、自定义注解
4、Aop自定义注解的应用(重点)

1.Java注解简介

1.1 Java注解(Annotation)

Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。

注解相关类都包含在java.lang.annotation包中。

1.2 Java注解分类

  1. JDK基本注解
  2. JDK元注解
  3. 自定义注解

1.3 JDK基本注解

  1. @Override 重写
  2. @Deprecated 已过时
  3. @SuppressWarnings(value = “unchecked”) 压制编辑器警告

2.注解的基本元素

声明一个注解要用到的东西

  • 修饰符
          访问修饰符必须为public,不写默认为pubic;
  • 关键字
          关键字为@interface;
  • 注解名称
          注解名称为自定义注解的名称,使用时还会用到;
  • 注解类型元素
          注解类型元素是注解中内容,可以理解成自定义接口的实现部分;

3.Java元注解

作用:元注解用于修饰其他的注解

3.1 @Retention:定义注解的保留策略

定义注解含义
@Retention(RetentionPolicy.SOURCE)//注解仅存在于源码中,在class字节码文件中不包含 ,在*.java编译文件中生效,如果一运行,这个注解会在*.class文件中自动消失,不会带到*.class中去
@Retention(RetentionPolicy.CLASS)//默认的保留策略,注解会在class字节码文件中编译存在,但运行时无法通过反射获取
@Retention(RetentionPolicy.RUNTIME)//注解会在class字节码文件中存在,在运行时可以通过反射获取到

3.2 @Target:指定被修饰的Annotation可以放置的位置(被修饰的目标)

指定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}),也就是此注解可以在方法和类上面使用

3.3 @Inherited:指定被修饰的Annotation将具有继承性 (判断自定义注解是否能继承)

案例:
在这里插入图片描述

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

4.自定义注解

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

  1. 标记Annotation(注解): 没有成员变量的Annotation; 这种Annotation仅利用自身的存在与否来提供信息(例如:)
    @Override(重写注解):不需要传递任何的参数的
    在这里插入图片描述

  2. 元数据Annotation(注解):包含成员变量的Annotation; 它们可以接受(和提供)更多的元数据;(例如:)
    @RequestMapping("/preSave")(请求地址映射的注解):可以接收很多的参数的
    在这里插入图片描述

4.2 如何自定义注解?

使用@interface关键字, 其定义过程与定义接口非常类似, 需要注意的是:

Annotation的成员变量在Annotation定义中是以无参的方法形式来声明的, 其方法名和返回值类型定义了该成员变量的名字和类型, 而且我们还可以使用default关键字为这个成员变量设定默认值;

注意:只有名字为“value”属性,赋值时可以省略属性名

接下来开始上代码:

5. 案例(三个)

5.1 案例一(获取类与方法上的注解值):

TranscationModel (定义枚举类):

package com.xiaoqing.ssm.P1;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 0:46
 *
 * 定义枚举类型
 */
public enum TranscationModel {
    Read, Write, ReadWrite
}

MyAnnotation1 :

package com.xiaoqing.ssm.P1;

import java.lang.annotation.*;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 0:47
 *
 *
 * MyAnnotation1注解可以用在类、接口、属性、方法上
 *  * 注解运行期也保留
 *  * 不可继承
 */

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

    String name();
}

MyAnnotation2 :

package com.xiaoqing.ssm.P1;

import java.lang.annotation.*;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 0:49
 *
 *
 * MyAnnotation2注解可以用在方法上
 *  *  注解运行期也保留
 *  *  不可继承
 */

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation2 {

    TranscationModel model() default TranscationModel.ReadWrite;
}

MyAnnotation3 :

package com.xiaoqing.ssm.P1;

import java.lang.annotation.*;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 0:50
 *
 * * MyAnnotation3注解可以用在方法上
 *  * 注解运行期也保留
 *  * 可继承
 */

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyAnnotation3 {

    TranscationModel[] models() default TranscationModel.ReadWrite;

}

Demo1 :获取类与方法上的注解值

package com.xiaoqing.ssm.P1;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 0:54
 *
 *
 * 一切的反射都是从获取类对象开始::
 *  1.getClass
 *  2.  .class
 *  3. class.forname
 *
 * 获取类与方法上的注解值
 */

@MyAnnotation1(name = "abc")
public class Demo1 {


    @MyAnnotation1(name = "xyz")
    private Integer age;

    @MyAnnotation2(model = TranscationModel.Read)
    public void list() {
        System.out.println("list");
    }

    @MyAnnotation3(models = {TranscationModel.Read, TranscationModel.Write})
    public void edit() {
        System.out.println("edit");
    }
}

测试结果:

Demo1Test :测试Demo1

package com.xiaoqing.ssm.P1;

import org.junit.Test;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 0:50
 *
 */
public class Demo1Test {
    @Test
    public void list() throws Exception {
//        获取类上的注解
        MyAnnotation1 annotation1 = Demo1.class.getAnnotation(MyAnnotation1.class);
        System.out.println(annotation1.name());//abc

//        获取方法上的注解
        MyAnnotation2 myAnnotation2 = Demo1.class.getMethod("list").getAnnotation(MyAnnotation2.class);
        System.out.println(myAnnotation2.model());//Read



    }

    @Test
    public void edit() throws Exception {
        MyAnnotation3 myAnnotation3 = Demo1.class.getMethod("edit").getAnnotation(MyAnnotation3.class);
        for (TranscationModel model : myAnnotation3.models()) {
            System.out.println(model);//Read,Write
        }
    }
}

list()方法:
在这里插入图片描述

edit()方法:
在这里插入图片描述

5.2 案例二(获取类属性上的注解属性值):

TestAnnotation(定义注解):

package com.xiaoqing.ssm.annotation.p2;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 16:29
 */
//@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TestAnnotation {

    String value() default "默认value值";

    String what() default "这里是默认的what属性对应的值";
}


Demo2:

package com.xiaoqing.ssm.annotation.p2;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 16:30
 *
 * 获取类属性上的注解属性值
 */
public class Demo2 {

    @TestAnnotation(value = "这就是value对应的值_msg1", what = "这就是what对应的值_msg1")
    private static String msg1;

    @TestAnnotation("这就是value对应的值1")
    private static String msg2;

    @TestAnnotation(value = "这就是value对应的值2")
    private static String msg3;

    @TestAnnotation(what = "这就是what对应的值")
    private static String msg4;
}

MyAnnotation2 :

package com.xiaoqing.ssm.P1;

import java.lang.annotation.*;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 0:49
 *
 *
 * MyAnnotation2注解可以用在方法上
 *  *  注解运行期也保留
 *  *  不可继承
 */

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation2 {

    TranscationModel model() default TranscationModel.ReadWrite;
}

MyAnnotation3 :

package com.xiaoqing.ssm.P1;

import java.lang.annotation.*;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 0:50
 *
 * * MyAnnotation3注解可以用在方法上
 *  * 注解运行期也保留
 *  * 可继承
 */

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyAnnotation3 {

    TranscationModel[] models() default TranscationModel.ReadWrite;

}

测试结果:

Demo2Test:测试Demo2

package com.xiaoqing.ssm.annotation.p2;

import org.junit.Test;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 16:30
 */
public class Demo2Test {

    @Test
    public void test1() throws Exception {
        TestAnnotation msg1 = Demo2.class.getDeclaredField("msg1").getAnnotation(TestAnnotation.class);
        System.out.println(msg1.value());
        System.out.println(msg1.what());
    }

    @Test
    public void test2() throws Exception{
        TestAnnotation msg2 = Demo2.class.getDeclaredField("msg2").getAnnotation(TestAnnotation.class);
        System.out.println(msg2.value());
        System.out.println(msg2.what());
    }

    @Test
    public void test3() throws Exception{
        TestAnnotation msg3 = Demo2.class.getDeclaredField("msg3").getAnnotation(TestAnnotation.class);
        System.out.println(msg3.value());
        System.out.println(msg3.what());
    }

    @Test
    public void test4() throws Exception{
        TestAnnotation msg4 = Demo2.class.getDeclaredField("msg4").getAnnotation(TestAnnotation.class);
        System.out.println(msg4.value());
        System.out.println(msg4.what());
    }
}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

5.3 案例三(获取参数修饰注解对应的属性值):

IsNotNull(定义注解):

package com.xiaoqing.ssm.annotation.p3;

import java.lang.annotation.*;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 16:36
 *
 * 非空注解:使用在方法的参数上,false表示此参数可以为空,true不能为空
 */

@Documented
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface IsNotNull {
    boolean value() default false;
}


Demo3:

package com.xiaoqing.ssm.annotation.p3;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 16:37
 *
 * 获取参数修饰注解对应的属性值
 */
public class Demo3 {
    public void hello1(@IsNotNull(true) String name) {
        System.out.println("hello:" + name);
    }

    public void hello2(@IsNotNull String name) {
        System.out.println("hello:" + name);
    }

    public void hello3(@IsNotNull(true) String name,@IsNotNull String sex,@IsNotNull(true) String addr) {
        System.out.println("hello:" + name);
    }
}


测试结果:

Demo3Test:测试Demo3

package com.xiaoqing.ssm.annotation.p3;

import org.junit.Test;

import java.lang.reflect.Parameter;

/**
 * @author晴sister
 * @site https://blog.csdn.net/men_ma
 * @company xxx公司
 * @create  2020-11-25 16:38
 */
public class Demo3Test {

    @Test
    public void hello1() throws Exception {
        Demo3 demo3 = new Demo3();
        for (Parameter parameter : demo3.getClass().getMethod("hello1", String.class).getParameters()) {
            IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);
            if(annotation != null){
                System.out.println(annotation.value());//true
            }
        }
    }

    @Test
    public void hello2() throws Exception {
        Demo3 demo3 = new Demo3();
        for (Parameter parameter : demo3.getClass().getMethod("hello2", String.class).getParameters()) {
            IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);
            if(annotation != null){
                System.out.println(annotation.value());//false
            }
        }
    }

    @Test
    public void hello3() throws Exception {
        Demo3 demo3 = new Demo3();
        for (Parameter parameter : demo3.getClass().getMethod("hello3", String.class,String.class,String.class).getParameters()) {
            IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);
            if(annotation != null){
                System.out.println(annotation.value());//true
            }
        }
    }
}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

6.自定义注解结合Aop数据字典应用

参考博客:
自定义注解结合Aop数据字典应用
自定义注解+拦截器+SSM应用于前端token校验

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值