详解java注解相关知识

网友投稿 295 2023-01-03

详解java注解相关知识

定义

1、如果注解中有属性,那么必须给属性赋值。

package com.lxc.Test;

// 定义一个注解

public @interface Annotation {

String name(); // 看似name像一个方法,实际上我们把name称为属性

}

使用上边注解:

package com.lxc.Test;

public class Test {

@Annotation(name="lxc")

public void test() {

}

}

2、如果注解中有属性,且没有定义默认值,那么在使用注解的时候,必须给属性赋值。

public @interface Annotation {

String name();

int age();

}

public class Test {

@Annotation(name="lxc", age=20)

public void test() {

}

}

但是注解中如果有默认值,在使用注解时,可以不给属性赋值

public class Test {

@Annotation(name="lxc")

public void test() {

}

}

public @interface Annotation {

String name();

String password() default "123";

}

3、value() 属性

如果注解中的一个属性名是value,且有且只有一个value(),在使用注解的时候,属性名可以省略

public class Test {

@Annotation("lxc")

public void test() {

}

}

public @interface Annotation {

String value();

String password() default "123";

}

注解中属性的类型有哪些

byte、short、int、float、double、boolean、char、String、Class、枚举

数组:

如果数组属性中有一个元素,那么数组的大括号可以省略:

public @interface Annotation {

String[] name();

}

public class Test {

// @Annotation(name={"lxc"}) // 写法一

@Annotation(name="lxc") // 写法二

public void test() {

}

}

枚举:

public enum MyEnum {

className, name, age,

}

public @interface Annotation {

String[] name();

MyEnum[] student();

}

public class Test {

@Annotation(name="lxc", student = {MyEnum.className, MyEnum.age})

public void test() {

}

}

注解如何使用:

(1)标记一个注解只能出现在类或者方法上

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

public @interface Annotation {

String userName();

MyEnum[] student();

}

(2)标记一个注解可以被反射机制所读取

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

// 标记注解只能出现在类上

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

// 标记注解可以被反射机制所读取

@Retention(RetentionPolicy.RUNTIME)

public @interface Annotation {

String userName();

MyEnum[] student();

}

获取注解中的属性值

通过反射机制来获取。

(1)获取类上边注解的属性:

注解类:

package com.lxc.Test;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

// 标记注解只能出现在类上

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

// 标记注解可以被反射机制所读取

@Retention(RetentionPolicy.RUNTIME)

public @interface Annotation {

String userName() default "吕星辰";

}

使用注解类:

// myAnnotation

@Annotation(userName = "哈哈")

http://public class myAnnotation {

}

获取注解类中 的属性:

package com.lxc.Test;

/**

* c.isAnnotationPresent(注解类.class) : 判断一个类上是否有注解,返回true、false

* c.getAnnotation(注解类.class) : 获取注解类的实例

*

*/

public class Test {

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

Class c = Class.forName("com.lxc.Test.myAnnotation");

System.out.println(c.isAnnotationPresent(Annotation.class));

// 判断一个类是否有:Annotation这个注解

if(c.isAnnotationPresent(Annotation.class)) {

// 获取注解对象

Annotation ann = (Annotation) c.getAnnotation(Annotation.class);

// 通过注解对象获取属性值

System.out.println(ann.userName());

}

}

}

(2)获取类中方法上边注解的属性:

注解类:

package com.lxc.Test;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

// 标记注解只能出现在类上

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

// 标记注解可以被反射机制所读取

@Retention(RetentionPolicy.RUNTIME)

public @interface Annotation {

String userName();

String password();

}

在方法上使用注解及获取方法上边的注解:

分析:想获取方法上的注解,首先需要获取该方法,获取该方法的前提,获取该方法的类:

package com.lxc.Test;

import java.lang.reflect.Method;

public class UserAnnotation {

@Annotation(userName = "lxc", password = "123")

public void getUser() {}

http:// public static void main(String[] args) throws Exception{

// 通过反射获取类

Class c = Class.forName("com.lxc.Test.UserAnnotation");

// 通过反射获取类中的方法

Method getUserMethod = c.getDeclaredMethod("getUser");

// 判断方法是否有 Annotation 这个注解

if(getUserMethod.isAnnotationPresent(Annotation.class)) {

Annotation ann = getUserMethod.getAnnotation(Annotation.class);

System.out.println(ann.userName()); // lxc

System.out.println(ann.password()); // 123

}

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Sentinel Dashboard限流规则保存方式
下一篇:天津快递物流查询单号查询(天津ems快递查询)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~