注解是什么?

①、引用自维基百科的内容:
java注解又称java标注,是jdk5.0版本开始支持加入源代码的特殊语法 元数据 。

java语言中的类、方法、变量、参数和包等都可以被标注。和javadoc不同,java标注可以通过反射获取标注内容。在编译器生成类文件时,标注可以被嵌入到字节码中。java虚拟机可以保留标注内容,在运行时可以获取到标注内容。 当然它也支持自定义java标注。

②、引用自网络的内容:
java 注解是在 jdk5 时引入的新特性,注解(也被称为 元数据 )为我们在代码中添加信息提供了一种形式化的方法,使我们可以在稍后某个时刻非常方便地使用这些数据。

元注解是什么?

元注解 的作用就是负责注解其他注解。java5.0定义了4个标准的meta-annotation(元注解)类型,它们被用来提供对其它 annotation类型作说明。

标准的元注解:

@target
@retention
@documented
@inherited
在详细说这四个元数据的含义之前,先来看一个在工作中会经常使用到的 @autowired 注解,进入这个注解里面瞧瞧: 此注解中使用到了@target、@retention、@documented 这三个元注解 。

@target({elementtype.constructor, elementtype.method, elementtype.parameter, elementtype.field, elementtype.annotation_type})
@retention(retentionpolicy.runtime)
@documented
public @interface autowired {
    boolean required() default true;
}

@target元注解:

@target注解,是专门用来限定某个自定义注解能够被应用在哪些java元素上面的,标明作用范围;取值在java.lang.annotation.elementtype 进行定义的。

public enum elementtype {
    /** 类,接口(包括注解类型)或枚举的声明 */
    type,

    /** 属性的声明 */
    field,

    /** 方法的声明 */
    method,

    /** 方法形式参数声明 */
    parameter,

    /** 构造方法的声明 */
    constructor,

    /** 局部变量声明 */
    local_variable,

    /** 注解类型声明 */
    annotation_type,

    /** 包的声明 */
    package
}

根据此处可以知道 @autowired 注解的作用范围:

// 可以作用在 构造方法、方法、方法形参、属性、注解类型 上
@target({elementtype.constructor, elementtype.method, elementtype.parameter, elementtype.field, elementtype.annotation_type})

@retention元注解:

@retention注解,翻译为持久力、保持力。即用来修饰自定义注解的生命周期。

注解的生命周期有三个阶段:

  • java源文件阶段;
  • 编译到class文件阶段;
  • 运行期阶段;

同样使用了retentionpolicy 枚举类型对这三个阶段进行了定义:

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文件中,但在运行时不会被虚拟机保留,这是一个默认的行为)
     */
    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.
     * (注解将被编译器记录在class文件中,而且在运行时会被虚拟机保留,因此它们能通过反射被读取到)
     * @see java.lang.reflect.annotatedelement
     */
    runtime
}

再详细描述下这三个阶段:

①、如果被定义为 retentionpolicy.source,则它将被限定在java源文件中,那么这个注解即不会参与编译也不会在运行期起任何作用,这个注解就和一个注释是一样的效果,只能被阅读java文件的人看到;

②、如果被定义为 retentionpolicy.class,则它将被编译到class文件中,那么编译器可以在编译时根据注解做一些处理动作,但是运行时jvm(java虚拟机)会忽略它,并且在运行期也不能读取到;

③、如果被定义为 retentionpolicy.runtime,那么这个注解可以在运行期的加载阶段被加载到class对象中。那么在程序运行阶段,可以通过反射得到这个注解,并通过判断是否有这个注解或这个注解中属性的值,从而执行不同的程序代码段。

注意:实际开发中的自定义注解几乎都是使用的 retentionpolicy.runtime 。

@documented元注解:

@documented注解,是被用来指定自定义注解是否能随着被定义的java文件生成到javadoc文档当中。

@inherited元注解:

@inherited注解,是指定某个自定义注解如果写在了父类的声明部分,那么子类的声明部分也能自动拥有该注解。

@inherited注解只对那些@target被定义为 elementtype.type 的自定义注解起作用。

自定义注解实现:

在了解了上面的内容后,我们来尝试实现一个自定义注解:

根据上面自定义注解中使用到的元注解得知:

①、此注解的作用范围,可以使用在类(接口、枚举)、方法上;

②、此注解的生命周期,被编译器保存在class文件中,而且在运行时会被jvm保留,可以通过反射读取;

自定义注解的简单使用:

上面已经创建了一个自定义的注解,那该怎么使用呢?下面首先描述下它简单的用法,后面将会使用其结合拦截器和aop切面编程进行实战应用;

应用场景实现

在了解了上面注解的知识后,我们乘胜追击,看看它的实际应用场景是肿么样的,以此加深下我们的理解;

实现的 demo 项目是以 springboot 实现的,项目工程结构图如下:

场景一:自定义注解 + 拦截器 = 实现接口响应的包装

使用自定义注解 结合 拦截器 优雅的实现对api接口响应的包装。

在介绍自定义实现的方式之前,先简单介绍下普遍的实现方式,通过两者的对比,才能更加明显的发现谁最优雅。

普通的接口响应包装方式:
现在项目绝大部分都采用的前后端分离方式,所以需要前端和后端通过接口进行交互;目前在接口交互中使用最多的数据格式是 json,然后后端返回给前端的最为常见的响应格式如下:

{
    #返回状态码
    code:integer,       
    #返回信息描述
    message:string,
    #返回数据值
    data:object
}

项目中经常使用枚举类定义状态码和消息,代码如下:

/**
 * @author 【 木子雷 】 公众号
 * @title: responsecode
 * @description: 使用枚举类封装好的响应状态码及对应的响应消息
 * @date: 2019年8月23日 下午7:12:50
 */
public enum responsecode {

    success(1200, "请求成功"),

    error(1400, "请求失败");


    private integer code;

    private string message;

    private responsecode(integer code, string message) {
        this.code = code;
        this.message = message;
    }

    public integer code() {
        return this.code;
    }

    public string message() {
        return this.message;
    }

}

同时项目中也会设计一个返回响应包装类,代码如下:

import com.alibaba.fastjson.jsonobject;
import java.io.serializable;

/**
 * @author 【 木子雷 】 公众号
 * @title: response
 * @description: 封装的统一的响应返回类
 * @date: 2019年8月23日 下午7:07:13
 */
@suppresswarnings("serial")
public class response<t> implements serializable {

    /**
     * 响应数据
     */
    private t date;

    /**
     * 响应状态码
     */
    private integer code;

    /**
     * 响应描述信息
     */
    private string message;

    public response(t date, integer code, string message) {
        super();
        this.date = date;
        this.code = code;
        this.message = message;
    }


    public t getdate() {
        return date;
    }

    public void setdate(t date) {
        this.date = date;
    }

    public integer getcode() {
        return code;
    }

    public void setcode(integer code) {
        this.code = code;
    }

    public string getmessage() {
        return message;
    }

    public void setmessage(string message) {
        this.message = message;
    }


    @override
    public string tostring() {
        return jsonobject.tojsonstring(this);
    }
}

最后就是使用响应包装类和状态码枚举类 来实现返回响应的包装了:

@getmapping("/user/findalluser")
public response<list<user>> findalluser() {
    logger.info("开始查询所有数据...");

    list<user> findalluser = new arraylist<>();
    findalluser.add(new user("木子雷", 26));
    findalluser.add(new user("公众号", 28));

    // 返回响应进行包装
    response response = new response(findalluser, responsecode.success.code(), responsecode.success.message());

    logger.info("response: {} \n", response.tostring());
    return response;
}

在浏览器中输入网址: http://127.0.0.1:8080/v1/api/user/findalluser 然后点击回车,得到如下数据:

{
    "code": 1200,
    "date": [
        {
            "age": 26,
            "name": "木子雷"
        },
        {
            "age": 28,
            "name": "公众号"
        }
    ],
    "message": "请求成功"
}

通过看这中实现响应包装的方式,我们能发现什么问题吗?

答:代码很冗余,需要在每个接口方法中都进行响应的包装;使得接口方法包含了很多非业务逻辑代码;

有没有版本进行优化下呢? en en 思考中。。。。。 啊,自定义注解 + 拦截器可以实现呀!

自定义注解实现接口响应包装:
①、首先创建一个进行响应包装的自定义注解:

/**
 * @author 【 木子雷 】 公众号
 * @package_name: com.lyl.annotation
 * @classname: responseresult
 * @description: 标记方法返回值需要进行包装的 自定义注解
 * @date: 2020-11-10 10:38
 **/
@target({elementtype.type, elementtype.method})
@retention(retentionpolicy.runtime)
@documented
public @interface responseresult {

}

②、创建一个拦截器,实现对请求的拦截,看看请求的方法或类上是否使用了自定义的注解:

/**
 * @author 【 木子雷 】 公众号
 * @package_name: com.lyl.interceptor
 * @classname: responseresultinterceptor
 * @description: 拦截器:拦截请求,判断请求的方法或类上是否使用了自定义的@responseresult注解,
 *               并在请求内设置是否使用了自定义注解的标志位属性;
 * @date: 2020-11-10 10:50
 **/
@component
public class responseresultinterceptor implements handlerinterceptor {

    /**
     * 标记位,标记请求的controller类或方法上使用了到了自定义注解,返回数据需要被包装
     */
    public static final string response_annotation = "response_annotation";

    /**
     * 请求预处理,判断是否使用了自定义注解
     */
    @override
    public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler)
            throws exception {
        // 请求的接口方法
        if (handler instanceof handlermethod) {
            final handlermethod handlermethod = (handlermethod) handler;
            final class<?> clazz = handlermethod.getbeantype();
            final method method = handlermethod.getmethod();
            // 判断是否在类对象上加了注解
            if (clazz.isannotationpresent(responseresult.class)) {
                // 在请求中设置需要进行响应包装的属性标志,在下面的responsebodyadvice增强中进行处理
                request.setattribute(response_annotation, clazz.getannotation(responseresult.class));
            } else if (method.isannotationpresent(responseresult.class)) {
                // 在请求中设置需要进行响应包装的属性标志,在下面的responsebodyadvice增强中进行处理
                request.setattribute(response_annotation, method.getannotation(responseresult.class));
            }
        }
        return true;
    }
}

③、创建一个增强controller,实现对返回响应进行包装的增强处理:

/**
 * @author 【 木子雷 】 公众号
 * @package_name: com.lyl.interceptor
 * @classname: responseresulthandler
 * @description: 对 返回响应 进行包装 的增强处理
 * @date: 2020-11-10 13:49
 **/
@controlleradvice
public class responseresulthandler implements responsebodyadvice<object> {

    private final logger logger = loggerfactory.getlogger(this.getclass());

    /**
     * 标记位,标记请求的controller类或方法上使用了到了自定义注解,返回数据需要被包装
     */
    public static final string response_annotation = "response_annotation";

    /**
     * 请求中是否包含了 响应需要被包装的标记,如果没有,则直接返回,不需要重写返回体
     *
     * @param methodparameter
     * @param aclass
     * @return
     */
    @override
    public boolean supports(methodparameter methodparameter, class<? extends httpmessageconverter<?>> aclass) {
        servletrequestattributes ra = (servletrequestattributes) requestcontextholder.getrequestattributes();
        httpservletrequest sr = (httpservletrequest) ra.getrequest();
        // 查询是否需要进行响应包装的标志
        responseresult responseresult = (responseresult) sr.getattribute(response_annotation);
        return responseresult == null ? false : true;
    }


    /**
     * 对 响应体 进行包装; 除此之外还可以对响应体进行统一的加密、签名等
     *
     * @param responsebody  请求的接口方法执行后得到返回值(返回响应)
     */
    @override
    public object beforebodywrite(object responsebody, methodparameter methodparameter, mediatype mediatype, class<? extends httpmessageconverter<?>> aclass, serverhttprequest serverhttprequest, serverhttpresponse serverhttpresponse) {
        logger.info("返回响应 包装进行中。。。");
        response response;
        // boolean类型时判断一些数据库新增、更新、删除的操作是否成功
        if (responsebody instanceof boolean) {
            if ((boolean) responsebody) {
                response = new response(responsebody, responsecode.success.code(), responsecode.success.message());
            } else {
                response = new response(responsebody, responsecode.error.code(), responsecode.error.message());
            }
        } else {
            // 判断像查询一些返回数据的情况,查询不到数据返回 null;
            if (null != responsebody) {
                response = new response(responsebody, responsecode.success.code(), responsecode.success.message());
            } else {
                response = new response(responsebody, responsecode.error.code(), responsecode.error.message());
            }
        }
        return response;
    }
}

④、最后在 controller 中使用上我们的自定义注解;在 controller 类上或者 方法上使用@responseresult自定义注解即可; 在浏览器中输入网址: http://127.0.0.1:8080/v1/api/user/findalluserbyannotation 进行查看:

// 自定义注解用在了方法上
@responseresult
@getmapping("/user/findalluserbyannotation")
public list<user> findalluserbyannotation() {
    logger.info("开始查询所有数据...");

    list<user> findalluser = new arraylist<>();
    findalluser.add(new user("木子雷", 26));
    findalluser.add(new user("公众号", 28));

    logger.info("使用 @responseresult 自定义注解进行响应的包装,使controller代码更加简介");
    return findalluser;
}

至此我们的接口返回响应包装自定义注解实现设计完成,看看代码是不是又简洁,又优雅呢。

总结:本文针对此方案只是进行了简单的实现,如果有兴趣的朋友可以进行更好的优化。

场景二:自定义注解 + aop = 实现优雅的使用分布式锁

分布式锁的最常见的使用流程:

先看看最为常见的分布式锁使用方式的实现,然后再聊聊自定义注解怎么优雅的实现分布式锁的使用。

普通的分布式锁使用方式:

通过上面的代码可以得到一个信息:如果有很多方法中需要使用分布式锁,那么每个方法中都必须有获取分布式锁和释放分布式锁的代码,这样一来就会出现代码冗余;

那有什么好的解决方案吗? 自定义注解使代码变得更加简洁、优雅;

自定义注解优雅的使用分布式锁:
①、首先实现一个标记分布式锁使用的自定义注解:

/**
 * @author 【 木子雷 】 公众号
 * @package_name: com.lyl.annotation
 * @classname: getdistributedlock
 * @description: 获取redis分布式锁 注解
 * @date: 2020-11-10 16:24
 **/
@target(elementtype.method)
@retention(retentionpolicy.runtime)
@documented
public @interface getdistributedlock {

    // 分布式锁 key
    string lockkey();

    // 分布式锁 value,默认为 lockvalue
    string lockvalue() default "lockvalue";

    // 过期时间,默认为 300秒
    int expiretime() default 300;

}

②、定义一个切面,在切面中对使用了 @getdistributedlock 自定义注解的方法进行环绕增强通知:

/**
 * @author: 【 木子雷 】 公众号
 * @package_name: com.lyl.aop
 * @classname: distributedlockaspect
 * @description: 自定义注解结合aop切面编程优雅的使用分布式锁
 * @date: 2020-11-10 16:52
 **/
@component
@aspect
public class distributedlockaspect {

    private final logger logger = loggerfactory.getlogger(this.getclass());

    @autowired
    redisservice redisservice;


    /**
     * around 环绕增强通知
     *
     * @param joinpoint 连接点,所有方法都属于连接点;但是当某些方法上使用了@getdistributedlock自定义注解时,
     *                  则其将连接点变为了切点;然后在切点上织入额外的增强处理;切点和其相应的增强处理构成了切面aspect 。
     */
    @around(value = "@annotation(com.lyl.annotation.getdistributedlock)")
    public boolean handlerdistributedlock(proceedingjoinpoint joinpoint) {
        // 通过反射获取自定义注解对象
        getdistributedlock getdistributedlock = ((methodsignature) joinpoint.getsignature())
                .getmethod().getannotation(getdistributedlock.class);

        // 获取自定义注解对象中的属性值
        string lockkey = getdistributedlock.lockkey();
        string lockvalue = getdistributedlock.lockvalue();
        int expiretime = getdistributedlock.expiretime();

        if (redisservice.trygetdistributedlock(lockkey, lockvalue, expiretime)) {
            // 获取分布式锁成功后,继续执行业务逻辑
            try {
                return (boolean) joinpoint.proceed();
            } catch (throwable throwable) {
                logger.error("业务逻辑执行失败。", throwable);
            } finally {
                // 最终保证分布式锁的释放
                redisservice.releasedistributedlock(lockkey, lockvalue);
            }
        }
        return false;
    }

}

③、最后,在 controller 中的方法上使用 @getdistributedlock 自定义注解即可;当某个方法上使用了 自定义注解,那么这个方法就相当于一个切点,那么就会对这个方法做环绕(方法执行前和方法执行后)增强处理;

在浏览器中输入网址: http://127.0.0.1:8080/v1/api/user/getdistributedlock 回车后触发方法执行:

// 自定义注解的使用
@getdistributedlock(lockkey = "userlock")
@getmapping("/user/getdistributedlock")
public boolean getuserdistributedlock() {
    logger.info("获取分布式锁...");
    // 写具体的业务逻辑

    return true;
}

通过自定义注解的方式,可以看到代码变得更加简洁、优雅。

场景三:自定义注解 + aop = 实现日志的打印

先看看最为常见的日志打印的方式,然后再聊聊自定义注解怎么优雅的实现日志的打印。

普通日志的打印方式:

通过看上面的代码可以知道,如果每个方法都需要打印下日志,那将会存在大量的冗余代码;

自定义注解实现日志打印:
①、首先创建一个标记日志打印的自定义注解:

/**
 * @author: 【 木子雷 】 公众号
 * @package_name: com.lyl.annotation
 * @classname: printlog
 * @description: 自定义注解实现日志打印
 * @date: 2020-11-10 18:05
 **/
@target(elementtype.method)
@retention(retentionpolicy.runtime)
@documented
public @interface printlog {

}

②、定义一个切面,在切面中对使用了 @printlog 自定义注解的方法进行环绕增强通知:

/**
 * @author: 【 木子雷 】 公众号
 * @package_name: com.lyl.aop
 * @classname: printlogaspect
 * @description: 自定义注解结合aop切面编程优雅的实现日志打印
 * @date: 2020-11-10 18:11
 **/
@component
@aspect
public class printlogaspect {

    private final logger logger = loggerfactory.getlogger(this.getclass());

    /**
     *  around 环绕增强通知
     *
     * @param joinpoint 连接点,所有方法都属于连接点;但是当某些方法上使用了@printlog自定义注解时,
     *                  则其将连接点变为了切点;然后在切点上织入额外的增强处理;切点和其相应的增强处理构成了切面aspect 。
     */
    @around(value = "@annotation(com.lyl.annotation.printlog)")
    public object handlerprintlog(proceedingjoinpoint joinpoint) {
        // 获取方法的名称
        string methodname = joinpoint.getsignature().getname();
        // 获取方法入参
        object[] param = joinpoint.getargs();

        stringbuilder sb = new stringbuilder();
        for (object o : param) {
            sb.append(o + "; ");
        }
        logger.info("进入《{}》方法, 参数为: {}", methodname, sb.tostring());

        object object = null;
        // 继续执行方法
        try {
            object = joinpoint.proceed();

        } catch (throwable throwable) {
            logger.error("打印日志处理error。。", throwable);
        }
        logger.info("{} 方法执行结束。。", methodname);
        return object;
    }

}

③、最后,在 controller 中的方法上使用 @printlog 自定义注解即可;当某个方法上使用了 自定义注解,那么这个方法就相当于一个切点,那么就会对这个方法做环绕(方法执行前和方法执行后)增强处理;

@printlog
@getmapping(value = "/user/findusernamebyid/{id}", produces = "application/json;charset=utf-8")
public string findusernamebyid(@pathvariable("id") int id) {
    // 模拟根据id查询用户名
    string username = "木子雷 公众号";
    return username;
}

④、在浏览器中输入网址: http://127.0.0.1:8080/v1/api/user/findusernamebyid/66 回车后触发方法执行,发现控制台打印了日志:

进入《findusernamebyid》方法, 参数为: 66; 
findusernamebyid 方法执行结束。。

使用自定义注解实现是多优雅,代码看起来简介干净,越瞅越喜欢;赶快去你的项目中使用吧, 嘿嘿。。。

以上就是java 自定义注解的魅力的详细内容,更多关于java 自定义注解的资料请关注www.887551.com其它相关文章!