java 注解有哪些_java 注解都有哪些东西?

展开全部

Java注解相当于对类或者方法或者变量额32313133353236313431303231363533e59b9ee7ad9431333262373363外的注释(标识)。以下是转载

-------------------------------------------------------

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 leizhimin 2009-12-18 14:15:46

*/

@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 leizhimin 2009-12-18 14:16:05

*/

@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 leizhimin 2009-12-18 15:23:12

*/

@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 leizhimin 2009-12-18 14:13:02

*/

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、当然,注解也可以没有定义成员,这样注解就成了一个标记符号了。

本回答由网友推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值