package com.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.FIELD) //该注解被标记在属性上 public @interface TestAnnField { public String value() default ""; }
package com.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 TestAnnoMethod { String types() default "";//设置默认值后,在使用注解时可以选择性的不写 types属性,如@TestAnnoMethod(username = "cyss") , 只写一个属性 String username() ; }
package com.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.TYPE) // 用于描述类型, 注释在类上面。 @Target(ElementType.FIELD) FIELD:用于描述域 ,注释在属性上面 /* * 1.CONSTRUCTOR:用于描述构造器 2.FIELD:用于描述域 3.LOCAL_VARIABLE:用于描述局部变量 * 4.METHOD:用于描述方法 5.PACKAGE:用于描述包 6.PARAMETER:用于描述参数 * 7.TYPE:用于描述类、接口(包括注解类型) 或enum声明 */ public @interface TestAnnoType { Class<?> type(); String method();//反回类型只能使用基本数据类型和数组 Class<?>[] args(); String value() default ""; }
package com.annotation; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; @TestAnnoType(args = { Test.class,Test2.class }, method = "getUserAll", type = Test.class) public class Test { @TestAnnField(value = "苹果") private String name; @TestAnnField(value = "sfd") private String age; @TestAnnField(value = "男") private String sex; private String adr; @TestAnnoMethod(username = "cyss") public void methodAnno(){ System.out.println("Method||01"); } @TestAnnoMethod(username = "xxxxxx") public void methodAnno2(){ System.out.println("Method||02"); } public static void main(String[] args){ Test.aminType(); try { Class<Test> c = Test.class; //获取类中指定的属性 Field field = c.getDeclaredField("name"); //判断是否包含此注解 if(field.isAnnotationPresent(TestAnnField.class)){ System.out.println("true"); } Test s = (Test)Class.forName("com.annotation.Test").newInstance(); System.out.println("属性:"+s.name); } catch (Exception e) { System.out.println("NO"); e.printStackTrace(); } } /** * 获取类上注解的值 */ public static void aminType(){ Test test = new Test(); try { Class<Test> c = Test.class; Class classs = Class.forName("com.annotation.Test"); TestAnnoType annoType = (TestAnnoType) classs.getAnnotation(TestAnnoType.class); String methodStr = annoType.method(); Class classType = annoType.type(); Class[] classTypes = annoType.args(); System.out.println("&&"+methodStr); System.out.println("&&"+classType); System.out.println("&&"+classTypes[0]); System.out.println("&&"+classTypes[1]); } catch (Exception e) { e.printStackTrace(); } } /** * 获取方法上注解的值 */ public static void mainMethod() { Test test = new Test(); try { //获取指定方法的注解值 Class<Test> c = Test.class; Method method = c.getMethod("methodAnno", new Class[] {}); TestAnnoMethod anoMethod = method.getAnnotation(TestAnnoMethod.class); String name = anoMethod.username(); System.out.println("methodAnno方法注解值:"+name); //所有方法的注解值 for (Method method2 : c.getDeclaredMethods()) { TestAnnoMethod anoMethod2 = method2.getAnnotation(TestAnnoMethod.class); if (anoMethod2 != null) { System.out.println("方法名:"+method2.getName()+"注解值:"+anoMethod2.username()); } } // 获取方法上的所有注解 Annotation[] annotations = method.getAnnotations(); for (Annotation annotation : annotations) { System.out.println("方法上的所有注解"+annotation); } } catch (Exception e) { e.printStackTrace(); } test.methodAnno(); } /** * 设置属性的值 */ public static void mainFields() { Test test = new Test(); try { Field[] methods = Test.class.getDeclaredFields(); for (Field method : methods) { boolean hasAnnotation = method .isAnnotationPresent(TestAnnField.class); if (hasAnnotation) { TestAnnField value = method .getAnnotation(TestAnnField.class); method.set(test, value.value()); } } } catch (Exception e) { e.printStackTrace(); } System.out.println("||"+test.name); System.out.println("||"+test.age); System.out.println("||"+test.sex); System.out.println("||"+test.adr); } }
相关推荐
java 自定义注解验证 可自己添加所需要的注解,本案例中只写了三个自定义注解类 本案例只花了半天时间,如有不好之处请多提提意见。谢谢!
包括Java自定义注解以及包括自定义注解的应用实例。以及注解相关知识的注解
自定义注解实现由类自定生成数据库中的表,适合各种类型的数据库,本案例主要实现的时sql server和oracle
java自定义注解实践,介绍详细!值得参考
简单demo,导入即可运行。
自定义注解和通过反射获取注解
3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑...
对某个类或某个方法进行注释,并且可以通过程序读取该注释来获取方法的用途或描述等。下面给出自定义注释的例子
这个一个自定义注解实现给注解传递伪动态参数的小案例,可以在此自定义注解的基础上来记录接口的调用记录。
Java基础复习笔记12Java自定义注解Annotation的使用
利用java注解机制,实现自定义注解实例,例子中实现了简单的注解赋值和获取并打印值。
内容:拦截器的使用很简单,定义一个自己的拦截器,向配置中添加一下就可以使用。为了方便,之后又引入了注解。本文就将用简洁的代码构建一个springboot的拦截器。...oNoNeedToken.java 自定义注解
通过一个超市的自动售卖机的例子,详细说明了java的自定义注解的使用。
java自定义 注解 annotation、标签库tag、监听listener、junit简单测试代码
主要介绍了java 自定义注解的实例详解的相关资料,需要的朋友可以参考下
Java自定义注解程序Demo
真实案例!java自定义注解实战
# 自定义注解的规则: - 1.定义注解 - 2.定义注解的切面 - 3.使用注解
自定义实现Autowired、Component、Configuration、SpringBootApplication注解,通过Java的反射完成。
java 元注解+拦截器实现自定义注解 @CmwAutoWired:自定义依赖注入 注意:注入的接口和实现类需要在同一包名下,注解的是类则无限制 @FieldAnnotation:自定义属性注解 @MethodAnnotation:自定义方法注解 @...