注解
1. java 注解:又称为java标记,jdk1.5开始引入的注释机制,注解可以通过反射获取标记的内容,编译器生成类字节码文件时,标记也可嵌入到字节码中。
2. java 内置注解:
-
使用在代码上的注解
-
@Override-检查方法是否被重写
public class Test extends AnnotationDemo { @Override public void test() { super.test(); } }
-
@Deprecated-标记过时
@Deprecated public String toLocaleString() { DateFormat formatter = DateFormat.getDateTimeInstance(); return formatter.format(this); }
-
@SuppressWarnings-忽略注解中声明的警告
-
-
元注解(DIV):
-
@Documented:标记这个注解是否包含在用户文档中
-
@Retention:标记注解保存的时间
-
SOURCE:源代码中
-
CLASS:字节码中
-
RUNTME:运行时
-
-
@Target:标记注解的使用目标
-
METHOD
-
FIELD
-
TYPE
-
-
@Inherited-标记注解是继承哪个注解类(默认是没有继承的)
-
- 自定义注解
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value() default "MyAnnotation"; } @MyAnnotation("eat1") public void eat(){ //s }
自定义注解
1. 案例:自定义关系型数据库的动态sql (mybatis)
select username,address,phone from user where username=zhangsan and password=123456