开发中自定义annotation的场景不太多,但是很多框架的源码里都用到了自定义annotation,不了解的话就看不懂了,所以也简单地学习一下
试了一下,比较简单,以下通过一个例子来说明
首先是annotation本身的定义
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ValueBind {
public FieldType type();
public String value();
}
这里用了一个枚举
public enum FieldType {
STRING, INT
}
下面是用到了该标签的类
public class Student {
private String id;
private String name;
private int age;
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("id: " + this.getId() + "\n");
sb.append("name: " + this.getName() + "\n");
sb.append("age: " + this.getAge() + "\n");
return sb.toString();
}
public String getId() {
return id;
}
@ValueBind(type = FieldType.STRING, value = "123")
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
@ValueBind(type = FieldType.STRING, value = "kyfxbl")
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@ValueBind(type = FieldType.INT, value = "29")
public void setAge(int age) {
this.age = age;
}
}
最后是执行的代码,可以看到效果
public class Main {
public static void main(String[] args) throws Exception {
Student student = new Student();
System.out.println(student.toString());
Method[] methodArray = student.getClass().getDeclaredMethods();
for (Method method : methodArray) {
if (method.isAnnotationPresent(ValueBind.class)) {
ValueBind annotation = method.getAnnotation(ValueBind.class);
FieldType type = annotation.type();
String value = annotation.value();
if (FieldType.INT.equals(type)) {
method.invoke(student, new Integer(value));
} else {
method.invoke(student, value);
}
}
}
System.out.println(student.toString());
}
}
我总的感觉,注解本身干不了啥,只能起到一个类型标识的作用(正如其命名annotation一样),然后代码里可以根据注解的类型,再进行需要的处理,相当于annotation起到的是类型标记的作用
不过在annotation里可以定义一些字段,这样可以传递参数过去
另外,注解也是可以“继承”的,比如说,在spring框架中,@Controller、@Service、@Repository,都认为是@Component
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
*/
String value() default "";
}
- 大小: 10.1 KB
分享到:
相关推荐
个人收集的相关Java Annotation的资源资料内容。
Java Annotation详解 Java Annotation详解 Java Annotation详解
JDK5.0 - Java Annotation 介绍 (ppt格式)
JAVA Annotation学习示例 的一个简单示例 有源码 也有相关博客说明
java之Annotation及其应用
Java Annotation注解技术
Java 5 annotation 学习笔记Java 5 annotation 学习笔记Java 5 annotation 学习笔记
java annotation 手册.doc,java annotation 手册.doc,java annotation 手册.doc
Java Annotation的讲解和例子~~~
本文针对java初学者或者annotation初次使用者全面地说明了annotation的使用方法、定义方式、分类。初学者可以通过以上的说明制作简单的annotation程序,但是对于一些高级的annotation应用(例如使用自定义annotation...
利用annotation(注解)开放简化java开放步骤
struts 拦截器,扩展,以及java annotation 这个是我学习所用,放在此处,作为中转。
Java annotation 什么是java annotation?annotation 的7种标注类型。nnotation提供了一条与程序元素关联任何信息或者任何元数据(metadata)的途径。从某些方面看,annotation就像修饰符一样被使用,并应用于包、...
如果你想知道java annotation是什么?你可以看看
JavaAnnotation必须掌握的特性Java开发Java经验技巧共6页.pdf.zip
java annotation手册 关于annotation是什么东西,百度一下会有很多关于它的描述,在这里就不用再copy了,我们还是以一个示例开始吧,在开始示例之前,我们先来一个小故事。
java1.5 annotation注释源代码,
javaAnnotation.pdf