注解_Annotation_内置注解_自定义注解_反射机制读取注解JAVA208-210
来源:互联网 发布:华傲数据是外包公司么 编辑:程序博客网 时间:2024/06/08 10:37
来源:http://www.bjsxt.com/
一、S02E208_01注解_Annotation、内置注解
什么是注解
内置注解
二、S02E209_01自定义注解
元注解
package com.test.annotation;@MyAnnotation01public class Demo01 { @MyAnnotation01(age=19,studentName="test",id=1001, schools={"北大","浙大"}) public static void main(String[] args) { } @MyAnnotation02(value="aaa")//或者("aaa") public void test(){ }}
package com.test.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(value={ElementType.METHOD,ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation01 { String studentName() default ""; int age() default 0; int id() default -1; String[] schools() default{"清华","复旦"};}
package com.test.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(value={ElementType.METHOD,ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation02 { String value();}
三、S02E210_01反射机制读取注解
注解作业
package com.test.annotation2;import java.lang.annotation.Annotation;import java.lang.reflect.Field;/** * 使用反射读取注解的信息,模拟处理注解信息的流程 */public class Demo { public static void main(String[] args) { try { Class clazz = Class.forName("com.test.annotation2.SxtStudent"); //获取类的所有有效注解 Annotation[] annotations = clazz.getAnnotations(); for (Annotation a : annotations) { System.out.println(a); } //获取类的指定的注解 SxtTable st = (SxtTable) clazz.getAnnotation(SxtTable.class); System.out.println(st.value()); //获取类的属性的注解 Field f = clazz.getDeclaredField("sname"); SxtField sxtField = f.getAnnotation(SxtField.class); System.out.println(sxtField.columnName()+"--"+sxtField.type()+"--"+sxtField.length()); //根据获取的表名和字段的信息,拼出DDL语句,然后,使用JDBC执行这个SQL,在数据库中生成相关的表 } catch (Exception e) { e.printStackTrace(); } }}
package com.test.annotation2;@SxtTable("tb_student")public class SxtStudent { @SxtField(columnName="id",type="int",length=10) private int id; @SxtField(columnName="sname",type="varchar",length=10) private String sname; @SxtField(columnName="age",type="int",length=3) private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStudentName() { return sname; } public void setStudentName(String studentName) { this.sname = studentName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
package com.test.annotation2;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(value={ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)public @interface SxtTable { String value();}
package com.test.annotation2;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(value={ElementType.FIELD})@Retention(RetentionPolicy.RUNTIME)public @interface SxtField { String columnName(); String type(); int length();}
控制台输出
@com.test.annotation2.SxtTable(value=tb_student)tb_studentsname--varchar--10
0 0
- 注解_Annotation_内置注解_自定义注解_反射机制读取注解JAVA208-210
- 反射机制读取自定义注解
- JAVA反射_注解
- Annotation(注解)_注解的自定义
- 反射机制_提高反射效率_操作泛型_操作注解JAVA213
- 初步认识注解,用反射来读取自定义注解
- 黑马程序员_反射、内省、注解
- 黑马程序员_高新技术:4) 注解、反射
- 黑马程序员_反射、内省、注解
- 使用反射读取注解
- 黑马程序员_注解
- 黑马程序员_注解
- 黑马程序员_注解
- 黑马程序员_注解
- 笔记_注解
- 黑马程序员_注解
- 黑马程序员_注解
- 黑马程序员_注解
- RPM 文件安装过程中 最后加上的--nodeps 和--force参数是什么意思
- C++第二次试验作业
- python压缩解压缩相关
- .Net MVC FTP文件下载
- Dagger2在AndroidStudio中的应用以及MVP
- 注解_Annotation_内置注解_自定义注解_反射机制读取注解JAVA208-210
- C++学习笔记
- NetBeans doc path
- zctf-pwn500-restaurant-write-up
- uploadify报错,自动请求框架的url地址
- github反应过慢,css加载失败问题
- Spark是否会替代Hadoop?
- HDU 1576 A/B 扩展欧几里得
- 使用百度CDN,搜索引擎抓取错误解决问题的经过