JAVA 注解示例 详解
2016-05-25 23:08
621 查看
JAVA 注解示例 详解
注解(Annotation)
注解(注释)基础知识点:
注解:也叫注释,也叫元数据。一种代码级别的说明。
作用分类:
编写文档:通过代码里标识的元数据生成文档【生成文档doc文档】
代码分析:通过代码里标识的元数据对代码进行分析【使用反射】
编译检查:通过代码里标识的元数据让编译器能过实现基本的编译检查【Override】
通过 解析注解 来使用这些数据。
注解的语法比较简单,除了@符号的使用以外,它基本上与java的固有语法一致,java内置了三种
注解,定义在java.lang包中。
@Override 表示当前方法是覆盖父类的方法。
@Deprecated 表示当前元素是不赞成使用的。
@SuppressWarnings 表示关闭一些不当的编译器警告信息。
deprecation,使用了过时的类或方法时的警告
unchecked,执行了未检查的转换时的警告
fallthrough,当 Switch 程序块直接通往下一种情况而没有 Break 时的警告
path,在类路径、源文件路径等中有不存在的路径时的警告
serial,当在可序列化的类上缺少serialVersionUID 定义时的警告
finally ,任何 finally 子句不能正常完成时的警告
all,关于以上所有情况的警告
下面是一个定义注解的实例
package Test_annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
/*
* 元注解@Target,@Retention,@Documented,@Inherited
*
* @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括:
* ElemenetType.CONSTRUCTOR 构造器声明
* ElemenetType.FIELD 域声明(包括 enum 实例)
* ElemenetType.LOCAL_VARIABLE 局部变量声明
* ElemenetType.METHOD 方法声明
* ElemenetType.PACKAGE 包声明
* ElemenetType.PARAMETER 参数声明
* ElemenetType.TYPE 类,接口(包括注解类型)或enum声明
*
* @Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括:
* RetentionPolicy.SOURCE 注解将被编译器丢弃
* RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃
* RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
*
* @Documented 将此注解包含在 javadoc 中
*
* @Inherited 允许子类继承父类中的注解
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
/*
* 定义注解 Test
* 注解中含有两个元素 id 和 desc
* desc 元素 有默认值
*/
4000
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
public int id() default 1;
public String desc() default "注解";
}
下面是一个使用注解 和 解析注解的实例
import java.lang.reflect.Method;
public class BeMyAnnotation {
@MyAnnotation(desc="添加方法")
public String add(){
return null;
}
@MyAnnotation(id=1, desc="删除方法")
public String delete(){
return null;
}
@MyAnnotation(id=1, desc="修改方法")
public String update(){
return null;
}
@MyAnnotation(id=1, desc="查询方法")
public String query(){
return null;
}
}
测试方法类:
/**
* @param args
*/
public static void main(String[] args) {
Method[] method = BeMyAnnotation.class.getMethods();
for(Method obj : method){
boolean bool = obj.isAnnotationPresent(MyAnnotation.class);
if(bool){
MyAnnotation annotation = obj.getAnnotation(MyAnnotation.class);
System.out.println("Test( method = " + obj.getName()
+ " , id = " + annotation.id() + " , description = "
+ annotation.desc() + " )");
}
}
}
注解(Annotation)
注解(注释)基础知识点:
注解:也叫注释,也叫元数据。一种代码级别的说明。
作用分类:
编写文档:通过代码里标识的元数据生成文档【生成文档doc文档】
代码分析:通过代码里标识的元数据对代码进行分析【使用反射】
编译检查:通过代码里标识的元数据让编译器能过实现基本的编译检查【Override】
通过 解析注解 来使用这些数据。
注解的语法比较简单,除了@符号的使用以外,它基本上与java的固有语法一致,java内置了三种
注解,定义在java.lang包中。
@Override 表示当前方法是覆盖父类的方法。
@Deprecated 表示当前元素是不赞成使用的。
@SuppressWarnings 表示关闭一些不当的编译器警告信息。
deprecation,使用了过时的类或方法时的警告
unchecked,执行了未检查的转换时的警告
fallthrough,当 Switch 程序块直接通往下一种情况而没有 Break 时的警告
path,在类路径、源文件路径等中有不存在的路径时的警告
serial,当在可序列化的类上缺少serialVersionUID 定义时的警告
finally ,任何 finally 子句不能正常完成时的警告
all,关于以上所有情况的警告
下面是一个定义注解的实例
package Test_annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
/*
* 元注解@Target,@Retention,@Documented,@Inherited
*
* @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括:
* ElemenetType.CONSTRUCTOR 构造器声明
* ElemenetType.FIELD 域声明(包括 enum 实例)
* ElemenetType.LOCAL_VARIABLE 局部变量声明
* ElemenetType.METHOD 方法声明
* ElemenetType.PACKAGE 包声明
* ElemenetType.PARAMETER 参数声明
* ElemenetType.TYPE 类,接口(包括注解类型)或enum声明
*
* @Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括:
* RetentionPolicy.SOURCE 注解将被编译器丢弃
* RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃
* RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
*
* @Documented 将此注解包含在 javadoc 中
*
* @Inherited 允许子类继承父类中的注解
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
/*
* 定义注解 Test
* 注解中含有两个元素 id 和 desc
* desc 元素 有默认值
*/
4000
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
public int id() default 1;
public String desc() default "注解";
}
下面是一个使用注解 和 解析注解的实例
import java.lang.reflect.Method;
public class BeMyAnnotation {
@MyAnnotation(desc="添加方法")
public String add(){
return null;
}
@MyAnnotation(id=1, desc="删除方法")
public String delete(){
return null;
}
@MyAnnotation(id=1, desc="修改方法")
public String update(){
return null;
}
@MyAnnotation(id=1, desc="查询方法")
public String query(){
return null;
}
}
测试方法类:
/**
* @param args
*/
public static void main(String[] args) {
Method[] method = BeMyAnnotation.class.getMethods();
for(Method obj : method){
boolean bool = obj.isAnnotationPresent(MyAnnotation.class);
if(bool){
MyAnnotation annotation = obj.getAnnotation(MyAnnotation.class);
System.out.println("Test( method = " + obj.getName()
+ " , id = " + annotation.id() + " , description = "
+ annotation.desc() + " )");
}
}
}
相关文章推荐
- spring mvc两大核心技术:1控制反转 2面向切面的编程
- Spring学习(六)实例演示Spring的使用
- Oauth2.0 用Spring-security-oauth2
- java String
- 为什么要使用MVC模式,MVC模式的优势有哪些?请写出你熟悉的MVC轻量级框架名称。
- java中数据类型转换
- SpringMVC学习--入门程序
- Spring Security 的login(登录页)重构
- Java之------socket网页蜘蛛(多线程版)爬邮箱
- 《Spring设计思想》AOP实现原理(基于JDK和基于CGLIB)
- Rxjava Subject分析
- 《Spring设计思想》AOP设计基本原理
- java 移动文件到其他文件夹(move)
- JAVA中的Random()函数
- Java设计模式(四):原型模式Prototype
- Java 内部类访问格式
- error和exception有什么区别?
- java的primitive主数据类型和引用
- 异常整理之:java.lang.NoClassDefFoundError:org/hamcrest/SelfDescribing
- struts2的一点知识(引入别人的项目,path问题,package的名字不能一样哦 )