码迷,mamicode.com
首页 > 编程语言 > 详细

Java注解Annotation学习笔记

时间:2017-04-06 23:17:11      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:string   表示   value   equal   retention   type   document   src   round   

一、自定义注解

1. 使用关键字 @interface
2. 默认情况下,注解可以修饰 类、方法、接口等。
3. 如下为一个基本的注解例子:
  1. //注解中的成员变量非常像定义接口
  2. public @interface MyAnnotation {
  3. //不带有默认值
  4. String name();
  5. //带有默认值
  6. int age() default 20;
  7. }
4. Reflect中3个常见方法解读
  • getAnnotion(Class<T> annotationClass) 返回该元素上存在的,指定类型的注解,如果没有返回null
  • Annotation[] getAnnotations() 返回该元素上所有的注解。
  • boolean isAnnotationPersent(Class<? extends Annotation> annotationClass) 判断该院上上是否含有制定类型的注释。

二、元注解

  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface ValueBind {
  4. filedType type();
  5. String value() default "SUSU";
  6. enum filedType {
  7. STRING, INT
  8. }
  9. }
其中@Target 和 @Retention为元注解。
  • @Retention有如下几种取值:
  • 技术分享
  • 其中通常都是RetentionPolicy.RUNTIME

 @Target有如下几个取值:
技术分享

@Inherited 表示该注解具有继承性。

三、Demo


1. ValueBind注解
  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface ValueBind {
  5. filedType type();
  6. String value() default "SUSU";
  7. enum filedType {
  8. STRING, INT
  9. }
  10. }


2. Person类
  1. public class Person implements Serializable {
  2. private String name;
  3. private int age;
  4. public String getName() {
  5. return name;
  6. }
  7. @ValueBind(type = ValueBind.filedType.STRING, value = "LCF")
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public int getAge() {
  12. return age;
  13. }
  14. @ValueBind(type = ValueBind.filedType.INT, value = "21")
  15. public void setAge(int age) {
  16. this.age = age;
  17. }
  18. @Override
  19. public String toString() {
  20. return "Person{" +
  21. "name=‘" + name + ‘\‘‘ +
  22. ", age=" + age +
  23. ‘}‘;
  24. }
  25. }

3. Main方法
  1. import java.lang.reflect.Method;
  2. public class Main {
  3. public static void main(String[] args) throws Exception {
  4. Object person = Class.forName("Person").newInstance();
  5. //以下获取的是:类的注解
  6. // Annotation[] annotations = person.getClass().getAnnotations();
  7. Method[] methods = person.getClass().getMethods();
  8. for (Method method : methods) {
  9. //如果该方法上面具有ValueBind这个注解
  10. if (method.isAnnotationPresent(ValueBind.class)) {
  11. //通过这个注解可以获取到注解中定义的
  12. ValueBind annotation = method.getAnnotation(ValueBind.class);
  13. if (annotation.type().equals(ValueBind.filedType.STRING)) {
  14. method.invoke(person, annotation.value());
  15. } else {
  16. method.invoke(person, Integer.parseInt(annotation.value()));
  17. }
  18. }
  19. }
  20. System.out.println(person);
  21. }
  22. }
输出结果:
Person{name=‘LCF‘, age=21}






Java注解Annotation学习笔记

标签:string   表示   value   equal   retention   type   document   src   round   

原文地址:http://www.cnblogs.com/LiuChunfu/p/6675920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!