java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。
注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。
1、元注解
元注解是指注解的注解。包括 @Retention @Target @Document @Inherited四种。
1.1、@Retention: 定义注解的保留策略
@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
1.2、@Target:定义注解的作用目标
其定义的源码为:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
由以上的源码可以知道,他的elementType 可以有多个,一个注解可以为类的,方法的,字段的等等
1.3、@Document:说明该注解将被包含在javadoc中
1.4、@Inherited:说明子类可以继承父类中的该注解
2、java 注解的自定义
下面是自定义注解的一个例子
@Documented
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Yts {
public enum YtsType{util,entity,service,model}
public YtsType classType() default YtsType.util;
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface HelloWorld {
public String name()default "";
}
@Retention(RetentionPolicy.RUNTIME)
定义的这个注解是注解会在class字节码文件中存在,在运行时可以通过反射获取到。
@Target({ElementType.TYPE,ElementType.METHOD})
因此这个注解可以是类注解,也可以是方法的注解
这样一个注解就自定义好了,当然注解里面的成员可以为基本的数据类型,也可以为数据,Object等等
3 注解是定义好了,那么怎么来得到,解析注解呢?
java的反射机制可以帮助,得到注解,代码如下:
public class ParseAnnotation {
public void parseMethod(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException{
Object obj = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});
for(Method method : clazz.getDeclaredMethods()){
HelloWorld say = method.getAnnotation(HelloWorld.class);
String name = "";
if(say != null){
name = say.name();
method.invoke(obj, name);
}
Yts yts = (Yts)method.getAnnotation(Yts.class);
if(yts != null){
if(YtsType.util.equals(yts.classType())){
System.out.println("this is a util method");
}else{
System.out.println("this is a other method");
}
}
}
}
@SuppressWarnings("unchecked")
public void parseType(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Yts yts = (Yts) clazz.getAnnotation(Yts.class);
if(yts != null){
if(YtsType.util.equals(yts.classType())){
System.out.println("this is a util class");
}else{
System.out.println("this is a other class");
}
}
}
}
前一个方法是解析得到方法注解的,后一个方法是得到类注解的
以下是测试方法类
@Yts(classType =YtsType.util)
public class SayHell {
@HelloWorld(name = " 小明 ")
@Yts
public void sayHello(String name){
if(name == null || name.equals("")){
System.out.println("hello world!");
}else{
System.out.println(name + "say hello world!");
}
}
}
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException {
ParseAnnotation parse = new ParseAnnotation();
parse.parseMethod(SayHell.class);
parse.parseType(SayHell.class);
}
【原文地址:http://blog.csdn.net/yixiaogang109/article/details/7328466】
分享到:
相关推荐
java 自定义注解验证 可自己添加所需要的注解,本案例中只写了三个自定义注解类 本案例只花了半天时间,如有不好之处请多提提意见。谢谢!
包括Java自定义注解以及包括自定义注解的应用实例。以及注解相关知识的注解
这个一个自定义注解实现给注解传递伪动态参数的小案例,可以在此自定义注解的基础上来记录接口的调用记录。
java自定义注解实践,介绍详细!值得参考
自定义注解实现由类自定生成数据库中的表,适合各种类型的数据库,本案例主要实现的时sql server和oracle
通过一个超市的自动售卖机的例子,详细说明了java的自定义注解的使用。
自定义注解和通过反射获取注解
简单demo,导入即可运行。
3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑...
Java基础复习笔记12Java自定义注解Annotation的使用
java 元注解+拦截器实现自定义注解 @CmwAutoWired:自定义依赖注入 注意:注入的接口和实现类需要在同一包名下,注解的是类则无限制 @FieldAnnotation:自定义属性注解 @MethodAnnotation:自定义方法注解 @...
利用java注解机制,实现自定义注解实例,例子中实现了简单的注解赋值和获取并打印值。
java自定义 注解 annotation、标签库tag、监听listener、junit简单测试代码
excel导入动态校验,自定义注解动态校验
主要介绍了java 自定义注解的实例详解的相关资料,需要的朋友可以参考下
自定义注解实现拦截sql,并在sql中增加相应的条件 。 对应博客: https://blog.csdn.net/qq_37716298/article/details/120659139
内容:拦截器的使用很简单,定义一个自己的拦截器,向配置中添加一下就可以使用。为了方便,之后又引入了注解。本文就将用简洁的代码构建一个springboot的拦截器。...oNoNeedToken.java 自定义注解
自定义注解结合Hutool对SpringBoot接口返回数据进行脱敏 自定义注解结合Hutool对SpringBoot接口返回数据进行脱敏 自定义注解结合Hutool对SpringBoot接口返回数据进行脱敏 自定义注解结合Hutool对SpringBoot接口返回...
Java自定义注解程序Demo
# 自定义注解的规则: - 1.定义注解 - 2.定义注解的切面 - 3.使用注解