“注解”有三种

1:无实际参数,只有声明

2:只存在单一参数,有些像方法调用

3:有多个参数

标准的“注解”就先不总结了。

想总结一下《如何创建自己的注解》。有很多流行的框架都会用到,所以对以后的学习也会有帮助。

1.无实际参数,只有声明(表达某种含义)

import java.lang.annotation.Retention;
import java.lang.annotation.Target
import java.lang.annotation.RetentionPolicy; /*
* 独自开发的注解
* @auther Z,wk
* @version 1.0
*
*/ @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface SampleRequired{
}

2.有单一参数或多个参数

package sample;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//ElementType.TYPE 类的定义
@Target({ 
  ElementType.TYPE,
ElementType.FIELD,
ElementType.CONSTRUCTOR,
ElementType.METHOD
})
@Retention(RetentionPolicy.RUNTIME)
public @interface Info {
   String value();
//此处还可以继续添加
//String value2();
}

3.使用方法

package sample;

import java.util.ArrayList;
import java.util.List;

// ElementType.TYPE :添加到类或接口的定义上
@Info("SampleClass1 Info")
public class SampleClass1 { private List list; public SampleClass1(){ } @Override
public boolean equals(Object obj){
return list.equals(obj);
}
  // ElmentType.Method:添加到方法声明上
@Info("hogehoge")
public void initList(){
list = new ArrayList();
list.add(10);
} }
package sample;

@Info("Sample2 class")
public class SampleClass2 {

//ElementType.FIELD : 添加到成员变量上
@Info("foo field")
private Foo foo;

//ElementType.CONSTRUCTOR : 添加到构造方法上
@Info("default constract")
public SampleClass2(){
foo = new Foo();
}

// ElementType.METHOD : 添加到方法声明上
@Info("bar method")
public void bar() {
foo.bar();
} }

4. 取得调用“注解”的对象(笔者理解为:很多框架使用注解的意义所在)

package sample;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method; public class Client {
public static void main(String[] args){
System.out.println("start annotation sample");
SampleClass1 sc1 = new SampleClass1();
SampleClass2 sc2 = new SampleClass2(); //取得指定的“注解”
Annotation annotList[] = Info.class.getAnnotations();
System.out.println("annotation size is [" + annotList.length + "]");
for(int i=0; i<annotList.length; i++){
Annotation anno = annotList[i];
System.out.println(" annotation class is [" + anno.annotationType().getName() + "]");
} /****取得带有注解的CLASS****/
//SampleClass1
Annotation annotList1[] = SampleClass1.class.getAnnotations();
System.out.println("SampleClass1's annotation size is [" + annotList1.length + "]");
for(int i=0; i<annotList1.length; i++){
Annotation anno1 = annotList1[i];
System.out.println(" annotation class is [" + anno1.annotationType().getName() + "]");
}
//SampleClass2
Annotation annotList2[] = SampleClass2.class.getAnnotations();
System.out.println("SampleClass2's annotation size is [" + annotList2.length + "]");
for(int i=0; i<annotList2.length; i++){
Annotation anno2 = annotList2[i];
System.out.println(" annotation class is [" + anno2.annotationType().getName() + "]");
}
System.out.println();
System.out.println(); /****取得带有注解的METHOD****/
//SampleClass1
Method methodList1[] = SampleClass1.class.getMethods();
System.out.println("SampleClass1's method count is [" + methodList1.length + "]");
for(Method method : methodList1){
System.out.println(" method name is [" + method.getName() + "]");
for(Annotation annot : method.getAnnotations()){
System.out.println(" method annotation is [" + annot.annotationType().getName() + "]");
}
}
//SampleClass2
Method methodList2[] = SampleClass1.class.getMethods();
System.out.println("SampleClass2's method count is [" + methodList2.length + "]");
for(Method method : methodList2){
System.out.println(" method name is [" + method.getName() + "]");
for(Annotation annot : method.getAnnotations()){
System.out.println(" method annotation is [" + annot.annotationType().getName() + "]");
}
}
System.out.println();
System.out.println(); /****取得带有注解的FIELD(成员变量)****/
//SampleClass1
Field fieldList1[] = SampleClass1.class.getFields();
System.out.println("SampleClass1 has [" + fieldList1.length + "] fields");
for(Field field : fieldList1){
System.out.println(" field name is [" + field.getName() +"]");
for(Annotation annot : field.getAnnotations()){
System.out.println(" field annotation is [" + annot.annotationType().getName() + "]");
}
}
//SampleClass1
Field fieldList2[] = SampleClass2.class.getDeclaredFields();//getFields();
System.out.println("SampleClass2 has [" + fieldList2.length + "] fields");
for(Field field : fieldList2){
System.out.println(" field name is [" + field.getName() +"]");
for(Annotation annot : field.getAnnotations()){
System.out.println(" field annotation is [" + annot.annotationType().getName() + "]");
}
}
System.out.println();
System.out.println(); } }

本人属于初学者,有不对的地方希望过路的大神批评指正。

java annotation(如何创建新的注解)小结的更多相关文章

  1. java使用io创建文件与删除文件的工具类

    java中对于文件的操作,是再常见不过了.以下代码是自己代码中所用到的工具类,仅供参考. import java.io.File; import java.io.IOException; /** * ...

  2. 第八篇:文件共享和使用 dup 函数创建新描述符的区别

    前言 文件共享是指同时打开一个文件 用 dup 函数能对指定文件描述符再创建一个新的描述符,且这个新的描述符和旧的描述符指向的是同一个文件. 这两种行为有什么区别呢?下面给出的两张文件系统的图形象的解 ...

  3. Java Annotation 注解

    java_notation.html div.oembedall-githubrepos { border: 1px solid #DDD; list-style-type: none; margin ...

  4. paip.Java Annotation注解的作用and 使用

    paip.Java Annotation注解的作用and 使用 作者Attilax 艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog. ...

  5. JAVA - Annotation 注解 入门

    Java注解提供了关于代码的一些信息,但并不直接作用于它所注解的代码内容.在这个教程当中,我们将学习Java的注解,如何定制注解,注解的使用以及如何通过反射解析注解. Java1.5引入了注解,当前许 ...

  6. java Annotation 注解的使用

    源码地址:https://github.com/yylxy/JavaAnnotationTest.git java Annotation 注解的使用 ,代码中有详细的注释.是用AndroidStudi ...

  7. Java Annotation 及几个常用开源项目注解原理简析

    PDF 版: Java Annotation.pdf, PPT 版:Java Annotation.pptx, Keynote 版:Java Annotation.key 一.Annotation 示 ...

  8. python与java的内存机制不一样;java的方法会进入方法区直到对象消失 方法才会消失;python的方法是对象每次调用都会创建新的对象 内存地址都不i一样

    python与java的内存机制不一样;java的方法会进入方法区直到对象消失 方法才会消失;python的方法是对象每次调用都会创建新的对象 内存地址都不i一样

  9. 自己写的基于java Annotation(注解)的数据校验框架

    JavaEE6中提供了基于java Annotation(注解)的Bean校验框架,Hibernate也有类似的基于Annotation的数据校验功能,我在工作中,产品也经常需要使 用数据校验,为了方 ...

  10. Java并发包——使用新的方式创建线程

    Java并发包——使用新的方式创建线程 摘要:本文主要学习了如何使用Java并发包中的类创建线程. 部分内容来自以下博客: https://www.cnblogs.com/dolphin0520/p/ ...

随机推荐

  1. TCP/IP协议(3): Wi-Fi(IEEE 802.11) 协议 —— 构成无线局域网的基本协议

    TCP/IP协议(3): Wi-Fi(IEEE 802.11) 协议 -- 构成无线局域网的基本协议 关于 Wi-Fi(IEEE 802.11) 协议 关于 IEEE 802.11 IEEE 802. ...

  2. Bulldog

    Bulldog 目录 Bulldog 1 信息收集 1.1 端口扫描 1.2 后台目录扫描 1.2.1 目录分析 2 Web-Shell利用 2.1 尝试命令执行 2.2 尝试利用系统命令注入 2.3 ...

  3. [网鼎杯2020]boom

    [网鼎杯2020]boom 分析 感觉比较友善,主要考查数学运算. 解析 解压之后发现是个exe文件,运行得到一个md5 查询: 输入明文得到: 下个挑战是解三元一次方程: 拿matlab算一下. s ...

  4. hdu-2544 最短路(SPFA)

    SPFA整体过程 1.用一个队列queue支撑. 2.dis[i]表示目前x到i的距离. 3.b[i]表示i是否在q中. 4.清空队列while(q.size()) q.pop();. 5.初始化(把 ...

  5. Educational Codeforces Round 137 (Rated for Div. 2) - E. FTL

    DP Problem - E - Codeforces 题意 有个 BOSS 有 \(H\;(1<=H<=5000)\) 血量,\(s\) 点防御 有两种武器可用攻击 BOSS,伤害分别为 ...

  6. 禅道服务崩溃 Can't init tc log

      0.环境 禅道 版本12.4.3 数据库 10.1.22-MariaDB SQL 服务器 192.168.0.82 centos 7 step 1.问题 磁盘爆满后,禅道在启动时报数据库 Can' ...

  7. 关于java业务限流组件的应用推广

    可参考的链接如下: 限流算法对比.网关限流实践总结(https://segmentfault.com/a/1190000020745218) 高并发下常见的限流算法(https://www.jians ...

  8. MYSQL --存储引擎的对比

    主要介绍三种 InnoDB .MyISAM .Memory 一.InnoDB 介绍: InnoDB是一种兼顾高可靠性和高性能的通用存储引擎,在MySQL5.5之后默认的存储引擎 特点: DML操作遵循 ...

  9. postgresql数据库插入和读取图片

    postgresql插入和读取图片 postgresql存储图片需要二进制类型bytea,创建一张测试表: postgres=# create table test_image(img bytea); ...

  10. Foxmail配置Exchange报错:RPC 服务器不可用 的解决方法

    如果确定了你电脑的RPC服务正常开启了,还是报这个错,那可能是你的Foxmail版本太低了, 低版本用Exchange时就会报这个错,升级为最新版就好了,我升级到7.2.23就正常了