反射和注解在java中偏高级用法,一般在各种框架中被广泛应用,文章简单介绍下反射和注解的用法,希望对你的工作学习有一定帮助
什么是注解
Java 注解也就是Annotation是从 Java5 开始引入的新技术
Annotation的作用:
Annotation的格式:
Annotation在里使用?
元注解
元注解的作用就是负责注解其他注解,java定义了4个标准的meta-annotation类型,被用来提供对其他annotation类型作说明
这些类型和它们所支持的类在java.lang.annotation包中可以找到(@Target,@Retention,@Documented,@Inherited)
自定义注解
使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口
public class Test03 { //注解可以显示赋值,如果没有默认值,一定要给注解赋值 @Myannotation2(name = "aj",schloos = {"机电学院"}) public void test(){ } @MyAnnotation3("") public void test2(){ } } @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @interface Myannotation2{ // 注解的参数,参数类型+参数名 String name() default ""; int age() default 0; //如果默认值为-1 代表不存在 int id() default -1; String[] schloos() ; } @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation3{ String value(); }
给代码加注解其实就是这么多,关键还是我们如何去读取注解,这就需要用到反射,下面重点介绍java反射
反射是java被视为动态语言的关键,反射机制允许程序在执行期借助Reflection API取得任何类的内部信息,并能直接操作任意对象内部熟悉及方法
Class c = Class.forName("java.lang.String")
加载完类之后,在堆内存的方法区就产生了一个Class类型的对象(一个类只有一个Class对象),这个对象就包含了完整的类的结构信息。我们可以通过这个对象看到类的结构。这个对象就像一面镜子,透过这个镜子看到类的结构,所以我们称之为:反射
Class类
对于每个类而言,JRE都为其保留一个不变的Class类型的对象,一个Class对象包含了特定某个结构的有关信息。
Class类的常用方法
反射获取对象
public class Test02 { public static void main(String[] args) throws ClassNotFoundException { Person person = new Student(); System.out.println("这个人是"+person.name); //通过对象获取 Class c1 = person.getClass(); System.out.println(c1.hashCode()); //通过forname获取 Class c2 = Class.forName("reflection.Student"); System.out.println(c2.hashCode()); //通过类名获取 Class c3 = Student.class; System.out.println(c3.hashCode()); //获得父类类型 Class c4 = c1.getSuperclass(); System.out.println(c4); } } @Data class Person{ public String name; public int age; } class Student extends Person{ public Student(){ this.name = "学生"; } } class Teacher extends Person{ public Teacher(){ this.name = "老师"; } }
反射操作方法、属性
public class Test03 { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException { Class c1 = Class.forName("reflection.Student"); Student student = (Student) c1.newInstance(); System.out.println(student.getName()); // 通过反射操作方法 Method setName = c1.getDeclaredMethod("setName", String.class); setName.invoke(student, "zhangshan"); System.out.println(student.getName()); Student student1 = (Student) c1.newInstance(); Field name = c1.getDeclaredField("name"); //反射不能直接操作私有属性,需要手动关掉程序的安全检测,setAccessible(true) name.setAccessible(true); name.set(student1,"lisi"); System.out.println(student1.getName()); } }
性能检测
public class Test04 { public static void test01(){ User user = new User(); long startTime = System.currentTimeMillis(); for (int i = 0; i <1000000000 ; i++) { user.getName(); } long endTime = System.currentTimeMillis(); System.out.println(endTime - startTime +"ms"); } public static void test02() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { User user = new User(); long startTime = System.currentTimeMillis(); Class c1 = user.getClass(); Method getName = c1.getDeclaredMethod("getName", null); for (int i = 0; i <1000000000 ; i++) { getName.invoke(user, null); } long endTime = System.currentTimeMillis(); System.out.println(endTime - startTime +"ms"); } public static void test03() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { User user = new User(); long startTime = System.currentTimeMillis(); Class c1 = user.getClass(); Method getName = c1.getDeclaredMethod("getName", null); getName.setAccessible(true); for (int i = 0; i <1000000000 ; i++) { getName.invoke(user, null); } long endTime = System.currentTimeMillis(); System.out.println(endTime - startTime +"ms"); } public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { test01(); test02(); test03(); } }
反射操作注解
public class Test05 { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { Class<?> c1 = Class.forName("reflection.Customer"); // 通过反射获取注解 Annotation[] annotations = c1.getAnnotations(); for (Annotation annotation:annotations){ System.out.println(annotation); } // 获取注解的值 TableAnnotation annotation = c1.getAnnotation(TableAnnotation.class); System.out.println(annotation.value()); //获取类指定注解 Field id = c1.getDeclaredField("id"); FiledAnnotation annotation1 = id.getAnnotation(FiledAnnotation.class); System.out.println(annotation1.columnName()); System.out.println(annotation1.length()); System.out.println(annotation1.type()); } } //类注解 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface TableAnnotation{ String value(); } @Data @TableAnnotation("db_customer") class Customer { @FiledAnnotation(columnName="id",type = "Long",length =10) private Long id; @FiledAnnotation(columnName="age",type = "int",length =10) private int age; @FiledAnnotation(columnName="name",type = "String",length =10) private String name; } //方法注解 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface FiledAnnotation{ String columnName(); String type(); int length(); }
到此这篇关于java高级用法之注解和反射的文章就介绍到这了,更多相关java注解和反射内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
免责声明:本站发布的内容(图片、视频和文字)以原创、来自互联网转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系QQ:712375056 进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
Copyright © 2009-2021 56dr.com. All Rights Reserved. 特网科技 特网云 版权所有 珠海市特网科技有限公司 粤ICP备16109289号
域名注册服务机构:阿里云计算有限公司(万网) 域名服务机构:烟台帝思普网络科技有限公司(DNSPod) CDN服务:阿里云计算有限公司 中国互联网举报中心 增值电信业务经营许可证B2
建议您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流浏览器浏览本网站