java注解理解

一、注解的作用:对类、方法、变量额外的注释(标识)

二、知识点:Annotation是Java5、6只后的新特征(中文称之为注解),并且越来越多的得到了应用,比如Spring、Hibernate3、Struts2、iBatis3、JPA、JUnit等中都得到了广泛应用,通过使用注解,代码的灵活性大大提高。 一般注解的使用都是在一些基础框架或者类库中来定义的。 注解的神秘之处在于:通过类似注释的方式,可以控制程序的一些行为、运行时的状态、可以为成员赋值,做配置信息等等,与常规编码思维大相径庭。
三、注解的用法:

      类注解、方法注解、字段(也称之域)注解的定义与使用

(一)、定义注解

package lavasoft.anntest;

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

/**
* 类注解
*
* @author ghp
*/

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotation4Class {
        public String msg();
}


/===========================================================/


package lavasoft.anntest;

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

/**
* 方法注解
*
* @author ghp
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation4Method {
        public String msg1();

        public String msg2();
}


/=============================================================/


package lavasoft.anntest;

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

/**
* 字段注解
*
* @author ghp
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyAnnotation4Field {
        public String commont();

        public boolean request();
}

(二)、写一个类,使用上述三类注解

package lavasoft.anntest;

/**
* 一个普通的Java类
*/
@MyAnnotation4Class(msg = "测试类注解信息")
class TestClass { 
       @MyAnnotation4Field(commont = "成员变量的注解信息", request = true)
        private String testfield;

        @MyAnnotation4Method(msg1 = "测试方法注解信息1", msg2 = "测试方法注解信息2")
        public void testMethod() {
                System.out.println("Hello World!");
        }
}

(三)、测试注解 (重点看看如何获取注解的信息)

为了使用注解,需要通过反射获取注解的对象。然后,再通过注解对象来操作注解信息。

package lavasoft.anntest;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
* 测试类
*
* @author ghp
*/
public class TestOptAnnotation {
        public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {
                TestClass t = new TestClass();
                System.out.println("-----------MyAnnotation4Class注解信息---------");
                MyAnnotation4Class an4clazz = t.getClass().getAnnotation(MyAnnotation4Class.class);
                System.out.println(an4clazz.msg());
                System.out.println("-----------MyAnnotation4Method注解信息---------");
                Method method = t.getClass().getMethod("testMethod", new Class[0]);
                MyAnnotation4Method an4method = method.getAnnotation(MyAnnotation4Method.class);
                System.out.println(an4method.msg1());
                System.out.println(an4method.msg2());
                System.out.println("-----------MyAnnotation4Field注解信息---------");
                Field field = t.getClass().getDeclaredField("testfield");
                MyAnnotation4Field an4field = field.getAnnotation(MyAnnotation4Field.class);
                System.out.println(an4field.commont());
                System.out.println(an4field.request());
        }
}

运行结果:
-----------MyAnnotation4Class注解信息---------
测试类注解信息
-----------MyAnnotation4Method注解信息---------
测试方法注解信息1
测试方法注解信息2
-----------MyAnnotation4Field注解信息---------
成员变量的注解信息
true

Process finished with exit code 0

四、总结

看完上面的测试过程,Annotation已经不再神秘了,这里总结下自定义注解的使用方法:

1、自定义注解,注意注解的时空范围,简单说就是注解针对的目标(类、方法、字段),以及注解的时效(是运行时有效还是在编译时有效)。

2、要获取注解的信息,必须通过Java的反射技术来获取Annotation对象,除此之外没有别的获取注解对象的方法。

3、获取了注解对象,就可以调用注解的方法来获取相对应的值了,为基础框架所用。

4、当然,注解也可以没有定义成员,这样注解就成了一个标记符号了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值