java注解是怎么实现的

java注解是怎么实现的,第1张

//每个注解就是一个类

@注解接口类(接口属性=值)

//创建一个自定义注解

@Retention(RetentionPolicyRUNTIME)    //运行时获得

@Target(ElementTypeMETHOD) //针对方法的注解

public @interface 自注标{

       int 属性1();

}

//注解加在自己的普通类上

class A{

  @自注标(属性1=123)   

   public void hello(){

   }

}

//在自己框架 *** 作下游开发者的代码时,通过反射得到该注解的值123

自注标 a=AgetClass()getMethod("hello")getAnnotation(自注标class);

int i=a属性1();//就能获得用户注解值。进行相应的动作

import javalangannotationRetention;

import javalangannotationRetentionPolicy;

import javalangreflectInvocationTargetException;

import javalangreflectMethod;

@Retention(RetentionPolicyRUNTIME)

@interface GetView {

String Method();

String Value();

}

public class Temp {

@GetView(Method = "aa", Value = "bb")

public void test() {

Systemoutprintln("In test method");

}

public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException {

Temp temp = new Temp();

Method method = tempgetClass()getMethod("test");

Systemoutprintln(methodisAnnotationPresent(GetViewclass));

methodinvoke(temp);

GetView view = methodgetAnnotation(GetViewclass);

Systemoutprintln(viewMethod());

Systemoutprintln(viewValue());

}

}

以上就是关于java注解是怎么实现的全部的内容,包括:java注解是怎么实现的、JAVA-Spring注解实现AOP权限拦截,如何取得方法上自定义Annotation的值呢、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存