各位大神,求救!!!java 如何获取该类上字段的注解,看图片

各位大神,求救!!!java 如何获取该类上字段的注解,看图片,第1张

我知道

1先获取这个类的class

Class<> objclass=tgetClass();

2 获取这个类的字段属性

Field[] at = objclassgetDeclaredFields();

3遍历所有字段

for (Field fd : at) {

//比如获取这个字段上是否包含NotNull

if (fdisAnnotationPresent(NotNullclass)) {

//这样就获取到这个注解属性了

NotNull d = fdgetAnnotation(NotNullclass);

}

}

4要获取一个注解,你要先获取他所在的字段

希望对你有帮助!

属于重点,在系统中用到注解权限时非常有用,可以精确控制权限的粒度

注意:要想使用反射去读取注解,必须将Retention的值选为Runtime Java代码import javalangannotationAnnotation;import javalangreflectMethod;//读取注解信息public class ReadAnnotationInfoTest {    public static void main(String[] args) throws Exception {        // 测试AnnotationTest类,得到此类的类对象        Class c = ClassforName(comiwtxokhtdannotationAnnotationTest);        // 获取该类所有声明的方法        Method[] methods = cgetDeclaredMethods();        // 声明注解集合        Annotation[] annotations;        // 遍历所有的方法得到各方法上面的注解信息        for (Method method : methods) {            // 获取每个方法上面所声明的所有注解信息            annotations = methodgetDeclaredAnnotations();            // 再遍历所有的注解,打印其基本信息            Systemoutprintln(methodgetName());            for (Annotation an : annotations) {                Systemoutprintln(方法名为: + methodgetName() + 其上面的注解为: + anannotationType()getSimpleName());                Method[] meths = anannotationType()getDeclaredMethods();                // 遍历每个注解的所有变量                for (Method meth : meths) {                    Systemoutprintln(注解的变量名为: + methgetName());                }            }        }    }}

// 定义注解并指定java注解保留策略为运行时RUNTIME,运行时注入到JAVA字节码文件里

// 这样才可以在运行时反射并获取它。

@javalangannotationRetention(javalangannotationRetentionPolicyRUNTIME)

@interface MyAnnotation{

String key() default "";

int value()  default 0; 

}

// 使用注解

@MyAnnotation(key="key1",value=200)

class MyClass{}

// 反射注解

public static void main(String[] args){

   MyClass myClass=new MyClass();

   MyAnnotation annotation=myClassgetClass()getAnnotation(MyAnnotationclass);

   Systemoutprintln("key="+annotationkey()+"\tvalue="+annotationvalue());

}

一、从注解中获取

使用注解方式,我们需要自定义一个注解,在注解中指定参数名,然后通过反射机制,获取方法参数上的注解,从而获取到相应的注解信息。这里自定义的注解是Param,通过value参数指定参数名,定义了一个工具类ParameterNameUtils来获取指定方法的参数名列表,这里获取测试类ParameterNameTest中定义的方法method1的参数名列表表,下面是具体的代码。

很多朋友都想知道java怎么获取注解的值?下面就一起来了解一下吧~

1、定义一个注解,用于给全局变量field字段赋值

package comhahastudyannotationvalue; import javalangannotationDocumented; import javalangannotationElementType; import javalangannotationRetention; import javalangannotationRetentionPolicy; import javalangannotationTarget; /  description: 定义一个注解,用于给 全局变量 field 字段 赋值,并使用反射取值。 

 特别提醒: @Rentention(RetentionPolicyRUNTIME) 时,注解才会被jvm加载,才能使用反射获取。  @version v10  @author w  @date 2018年8月1日下午2:37:40 / @Documented @Retention(RetentionPolicyRUNTIME) @Target(value=ElementTypeFIELD) public @interface Fields { int sort() default 0 ; String value() ; }

2、创建一个普通的类,使用 @ConsAnnotation、@Fields 注解

package comhahastudyannotationvalue; /  description: 创建一个普通的类,使用 @ConsAnnotation、@Fields 注解。  @version v10  @author w  @date 2018年8月1日下午2:50:23 / @ConsAnnotation(request = { "hello","world","annotation!" }) public class User { @Fields("中华人民共和国") private String userName; public String getUserName() { return userName; } public void setUserName(String userName) { thisuserName = userName; } }

3、针对 comhahastudyannotationvalueUser 类使用注解的测试

package comhahastudyannotationvalue; import javalangreflectField; import javautilArrays; /  description: 针对 comhahastudyannotationvalueUser 类使用注解的测试  @version v10  @author w  @date 2018年8月1日下午2:37:13 / public class ValueTest { public static void main(String[] args) throws Exception { User user = new User(); // 1、 获取 User类上的注解 @ConsAnnotation ConsAnnotation anno = usergetClass()getAnnotation(ConsAnnotationclass); String[] arr = annorequest(); Systemoutprintln(ArraystoString(arr)); // [hello, world, annotation!] // 2、 获取User类中 private String userName; 变量上的注解 @Field Field f = usergetClass()getDeclaredField("userName"); Fields anno2 = fgetAnnotation(Fieldsclass); usersetUserName(anno2value()); Systemoutprintln(usergetUserName()); // 中华人民共和国 } }

定义:注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK15及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。

WebApplicationContext wac = WebApplicationContextUtilsgetRequiredWebApplicationContext(getServletContext()); 有WebApplicationContext 了对象了 spring托管的所有对象都可以拿到了。 当然不推荐这种方式,一般是注入的方式,特殊情况下

以上就是关于各位大神,求救!!!java 如何获取该类上字段的注解,看图片全部的内容,包括:各位大神,求救!!!java 如何获取该类上字段的注解,看图片、Java 注解的读取注解信息的方法、java反射机制 怎样获取到类上面的注解等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9652331.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存