java annotations详解_Java Annotations

Java常见注解

JDK自带注解

@Override:重写

@Deprecated:过时

@Suppvisewarnings:忽略警告

常见第三方注解 > Spring

@Autowired

@Service

@Respository

常见第三方注解 > Mybatis

@InsertProvider

@UpdateProvider

@Options

注解分类

按照运行机制分

源码注解:注解只在源码中存在,编译成.class文件就不存在了。

编译时注解:注解在源码和.class文件中都存在。(@Override,@Deprecated,@Suppvisewarnings)

运行时注解:在运行阶段还起作用,甚至会影响运行逻辑。(@Autowired)

按照来源分

来自JDK的注解

来自第三方的注解

自定义的注解

元注解

自定义注解

自定义注解语法要求

使用@interface关键字定义注解。

成员以无参无异常方式声明,可以用default为成员指定一个默认值,成员类型是受限制的,包括基本数据类型,String,Class,Annotation,Enumeration。

如果注解只有一个成员,则成员必须取名为value(),在使用时可以忽略成员名和赋值号(=)。

注解类可以没有成员,没有成员的注解称为标识注解。

元注解 @Target:注解的作用域,包括CONSTRUCTOR(构造方法声明),FIELD (字段声明),LOCAL_VARIABLE(局部变量声明),METHOD(方法声明),PACKAGE(包声明),PARAMETER(参数声明),TYPE(类,接口)。

元注解 @Retention:注解的生命周期,包括SOURCE(只在源码显示,编译时会丢弃),CLASS(编译时会记录到class中,运行时忽略),RUNTIME(运行时存在,可以通过反射读取)。

元注解 @Inherited:允许子类继承。

元注解 @Documented:生成javadoc时会包含注解。

import java.lang.annotation.*;

@Target({ElementType.METHOD, ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Inherited

@Documented

public @interface Description {

String desc();

String author();

int age() default 18;

}

使用自定义注解

( = , = , ...)

@Description(desc = "hello desc", author = "tom", age = 19)

public void test() {

}

解析注解

通过反射获取类,函数或成员上的运行时注解信息,从而实现动态控制程序的逻辑。

子类注解优先级高于父类注解。

定义注解

import java.lang.annotation.*;

@Target({ElementType.METHOD, ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Inherited

@Documented

public @interface Description2 {

String value();

}

引用注解

@Description2("I am class Person annotation")

public class Person {

@Description2("I am class Person : method name annotation")

public String name() {

return null;

}

@Description2("I am class Person : method age annotation")

public int age() {

return 0;

}

public void sing() {

}

}

@Description2("I am class Man annotation")

public class Man extends Person {

@Override

@Description2("I am class Man : method name annotation")

public String name() {

return super.name();

}

}

解析注解 & 运行结果

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

public class Test {

public static void main(String[] args) throws ClassNotFoundException {

// 使用类加载器加载类

Class> c = Class.forName("annotation.Man");

// 找到类上面的注解

boolean isPresent = c.isAnnotationPresent(Description2.class);

// 获取注解实例

if (isPresent) {

Description2 desc2 = c.getAnnotation(Description2.class);

System.out.println(desc2.value());

}

// 找到方法上的注解

Method[] methods = c.getMethods();

// 获取注解实例 1

for (Method method : methods) {

boolean isMPresent = method.isAnnotationPresent(Description2.class);

if (isMPresent) {

Description2 mDesc2 = method.getAnnotation(Description2.class);

System.out.println("1:" + mDesc2.value());

}

}

// 获取注解实例 2

for (Method method : methods) {

Annotation[] annotations = method.getAnnotations();

for (Annotation annotation : annotations) {

if (annotation instanceof Description2) {

Description2 desc2 = (Description2) annotation;

System.out.println("2:" + desc2.value());

}

}

}

}

}

I am class Man annotation

1:I am class Man : method name annotation

1:I am class Person : method age annotation

2:I am class Man : method name annotation

2:I am class Person : method age annotation

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值