什么是注解?
是元数据(Annotation) ,是一种对代码进行说明的数据,JDK1.5引入的新规范,是与类、接口、枚举等引用类型处于同一个层次,可以用在包、类、属性、方法、方法的参数和局部变量等的前边,不影响代码的语义,可以对其进行解析
注解Annotation: 是一种引用数据类型(在类加载的时候会生成字节码文件)
注解在以后的用途:
注解的作用:
注解只能起到一个标识的作用
spring框架:基于注解的开发模式
学习了反射可以不使用new关键字去创建一个类的实例
spring是一个容器里面会存放很多类的实例
我怎么知道有哪些类需要去创建实例,那些类不需要创建实例?
用注解去标识一下,需要创建实例的类,用注解去做一个标识
在spring框架检测到该类有注解时,就会通过反射机制去创建该类的实例
beans: User、Student、Person、Animal
xxxxxxxxxx31public @interface MyAnnotation {2 3}注解中可以拥有属性,但是不能拥有方法
如果你给一个注解声明了属性,那么在注解修饰元素的时候,就必须要给属性赋值,除非你在声明属性的时候,给了属性默认值
xxxxxxxxxx151public @interface MyAnnotation {2
3 //在注解中如何去声明属性4 String name() default "";5
6 int i();7
8 String[] strings() default {"1","2"};9
10 //枚举类型11 Math math() default Math.Max;12 13 Math[] maths() default {Math.Max,Math.Count,Math.Sum};14 15}
在注解中定义属性只能是八大基本数据类型、字符串类型String、枚举类型、以及主要类型所对应的数组类型
在定义属性时,如果属性名称是value,那在注解修饰元素,并且给属性赋值的时候,属性名称可以省略不写
xxxxxxxxxx11(name = "123",i = 100)
注解使用时的语法格式是:
@注解类型名
注解可以出现在类上、属性上、方法上、变量上等
注解可以出现在注解类型上
Deprecated 用 @Deprecated 注释的程序元素,不鼓励程序员使用这样的元素,通过是因为它很危险或存在更好的选择
Override表示一个方法声明打算重写超类中的另一个方法声明
SuppressWarnings 指示应该在注释元素(以及包含在该注释元素中的所有程序元素)中取消显示指定的编译器警告
什么是元注解?
用来标注“注解类型”的“注解”,称为元注解
修饰注解的注解
在定义注解时需要通过元注解指定例如注解的使用场合、是否生成文档、是否可以继承、注解保持的策略(是否能被反射机制所获取到)
元注解:
@Target({ElementType.METHOD,ElementType.TYPE}) 表示注解的作用域
参数只能是枚举类型数组
xxxxxxxxxx51//使得注解能标识在类和属性前2({ElementType.TYPE,ElementType.FIELD})3public @interface MyAnnotation {4
5}
@Retention(RetentionPolicy.RUNTIME)
注解的生命周期
RetentionPolicy(设置注解的生命周期以及保持性策略):
SOURCE:
当前注解只保留在源码这个层面上,在类加载的时候不会去生成字节码文件,而且不可以被反射机制所获取!
CLASS:
在类加载的时候会生成字节码文件,但是不可以被发射机制获取
RUNTIME:
在类加载的时候可以生成字节码文件,而且可以被反射机制获取工
@Inherited
标识允许子注解来继承这个注解(注意这里只能是继承不能是接口上使用,而且只能继承类上的注解,不能继承方法上的注解)
@Documented
生成JAVADOC时会包含注解
1、创建类对应的字节码对象
2、通过字节码对象获取注解字节码对象
xxxxxxxxxx431public class Annotation01 {2
3 public static void main(String[] args) {4
5
6 try {7
8 //创建字节码对象9 Class aClass = Class.forName("com.os467.pojo.User");10
11 //获取类上的注解12 MyAnnotation annotation = (MyAnnotation)aClass.getAnnotation(MyAnnotation.class);13
14 if (annotation != null){15
16 //获取注解上的属性17 System.out.println(annotation.name());18
19 //获取属性字节码对象20 Field[] declaredFields = aClass.getDeclaredFields();21
22 for (Field declaredField : declaredFields) {23
24 MyAnnotation annotation1 = declaredField.getAnnotation(MyAnnotation.class);25
26 if (annotation1 != null){27
28 System.out.println(declaredField.getName()+" 这个属性上有注解");29
30 }31
32 }33
34 }35
36 } catch (ClassNotFoundException e) {37 e.printStackTrace();38 }39
40
41 }42
43}
xxxxxxxxxx1001package com.os467;2
3import com.os467.Annotion.Table;4
5import java.lang.annotation.Annotation;6import java.lang.reflect.Field;7
8
9public class ORM {10
11 private ORM(){12
13 }14
15 public static String getSql(Object obj){16
17 StringBuilder stringBuilder = new StringBuilder();18
19 //获取obj字节码对象20 Class aClass = obj.getClass();21
22 Table annotation = (Table)aClass.getAnnotation(Table.class);23
24 //表的名称25 String table = null;26
27 if (annotation != null){28
29 //获取表的名称30 table = annotation.value();31
32 stringBuilder.append("select * from "+ table +" where ");33
34 }35
36 //获取属性字节码对象37 Field[] declaredFields = aClass.getDeclaredFields();38
39 for (Field declaredField : declaredFields) {40
41 //打破封装42 declaredField.setAccessible(true);43
44 com.os467.Annotion.Field annotation1 = declaredField.getAnnotation(com.os467.Annotion.Field.class);45
46 //注解不为空的情况下拼接47 if (annotation1 != null){48
49 try {50
51 //获取属性值52 Object o = declaredField.get(obj);53
54 //检查email属性是否有多个","隔开55 if (o != null && o.toString().contains(",")){56
57 stringBuilder.append(annotation1.value() + " in(" + o + ") and");58
59 }else if (o != null && !o.toString().equals("0")){60
61 stringBuilder.append(annotation1.value() + " = ");62
63 if (o instanceof String){64
65 stringBuilder.append("'" + o + "'" + " and ");66
67 }68
69 if (o instanceof Integer){70
71 stringBuilder.append(o + " and ");72
73 }74
75 }76
77
78 } catch (IllegalAccessException e) {79 e.printStackTrace();80 }81
82 }83
84 }85
86 String s = stringBuilder.toString();87
88 //替换掉最后的and 89 s = s.substring(0,s.length() - 4);90
91 if (s.contains("=")){92
93 return s;94
95 }96
97 return null;98 }99
100}