Java注解

一、 什么是注解

我们在使用SrpingBoot、Mybatis这些框架的时候可以看到很多注解,例如@EnableAutoConfig、@Select等,这些都是注解。已知注释是给人阅读的文字,而注解不仅可以给人阅读,也可以被编译器读取。

注解可以附在package,class,method,field等位置。注解的定义和使用与Java的反射机制息息相关。

二、内置注解

Java语言已经在Java.lang包中已经预定义了一些注解,下面是几个常见的内置注解。

1. Override

@Override被用于重写父类或实例化接口的方法中,作为一种声明和提醒作用,表明这个方法在父类中已经被定义或在接口中已经被声明的方法,在实际开发中主要用于提示,提升代码可读性。

2. Deprecated

@Deprecated用于声明方法、类或属性已经过时,提醒其他程序员不推荐使用@Deprecated注解所标注的方法、类或属性,或者存在其他更好的选择。

3. SuppressWarnings

顾名思义,@SuppressWarnings用于抑制编译时的警告信息,可以标记在类上或属性上。与上述两个注解不同@SuppressWarnings注解有参数,下面是一些常用参数

  • @SuppressWarnings(“all”) 抑制所有警告
  • @SuppressWarnings(“unused”) 抑制未使用变量的警告
  • @SuppressWarnings(“unchecked”) 抑制没有进行类型检查操作的警告
  • @SuppressWarnings(value={“unchecked”,“unused”}) 组合使用

三、元注解

元注解的作用是负责注解其他的注解,一般在定义自定义注解时使用。元注解有@Target、@Retention、@Documented、@Inheriterd等。

1. Target

@Target注解用于指定注解的使用范围,即注解可以用于哪些程序元素上。常用参数有:

  • @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)注解可以标记在包上。

2. Retention

@Retention用于指定注解的生命周期,即注解在代码中的存在范围。常用参数有:

  • @Retention(RetentionPolicy.SOURCE) 注解仅在源代码中存在,编译后会被丢弃。
  • @Retention(RetentionPolicy.CLASS) 注解在编译时被保留,但不会被加载进 JVM 运行时环境。
  • @Retention(RetentionPolicy.RUNTIME) 注解在运行时被保留,可以被反射机制读取到

3. Document

表明将该注解生成到Java doc中

4. Inherited

表明子类可以继承父类的注解

四、如何自定义注解

上面学习完一些基本概念后,我们开始自定义一些注解。
自定义注解使用@interface修饰,自动继承java.lang,annotation.Annotation接口。
1.无参注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Mlog {
}

//调用
@Mlog
  1. 有参注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Mlog {
	//value是Mlog注解类的成员,默认值是""。其中括号是必须的
	String value() default "";
}

//调用,如果只用一个参数的话value可以省略
@Mlog(value={"This is Mlog"})
//直接使用呢默认参数
@Mlog 

五、在项目中的用法

注解一般配合AOP(面相切面编程)使用。下面的代码是AOP+注解的项目例子。当在函数上添加注解时会在终端输出函数名称。
项目的地址在github

  1. Mlog.java
package com.example.annotationtest.annotation;

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

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Mlog {
}
  1. MyAspect.java
package com.example.annotationtest.aspect;


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.springframework.stereotype.Component;

@Aspect
@Component
public class MyAspect {
	//定义切面点
    @Pointcut("@annotation(com.example.annotationtest.annotation.Mlog)")
    public void myPointCut(){}
	
	//在前面点调用之前执行beforeMlog函数
    @Before("myPointCut()")
    public void beforeMlog(JoinPoint joinPoint){
        System.out.println("Name is "+joinPoint.getSignature().getName()); //输出函数名
        //输出这个函数在那个类中
        System.out.println("DeclaringType Name is "+joinPoint.getSignature().getDeclaringType().getName()); 
        //输出函数返回值,包+函数定义的类+函数名,参数类型
        System.out.println("toLongStringis "+joinPoint.getSignature().toLongString());
        //输出类+函数名
        System.out.println("toShortString "+joinPoint.getSignature().toShortString());
        //输出函数修饰符
        System.out.println("Modifiers is "+joinPoint.getSignature().getModifiers());
    }
}

  1. 测试函数
package com.example.annotationtest.controller;

import com.example.annotationtest.annotation.Mlog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @GetMapping("logtest")
    @Mlog
    public String logtest(){
        System.out.println("The message is in logtest function");
        return "This is longtest";
    }
}

结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值