在创建ejb组件时,必需提供一些定义,使得ejb组件使用一些服务例如:安全服务,持久化服务,事务服务。ejb容器可以提供这些服务,这样ejb只要实现业务逻辑就可以了。但是说到底ejb容器使用ejb组件的元数据来提供这些服务,在以前ejb的元数据是以xml配置文件形式出现的,这些配置文件与ejb源文件是分开的。
ejb的部署人员无法了解ejb本身的信息,如果ejb组件的创建者用注释(annotation)的方法将这些配置服务的信息和代码放在一起,这样ejb的部署者就可以了解ejb的信息,ejb的home接口可以使用annotation自动生成,当然到目前为止更好的是在简单的java object上使用annotations。
一 什么是annotation
在已经发布的jdk1.5(tiger)中增加新的特色叫 annotation。annotation提供一种机制,将程序的元素如:类,方法,属性,参数,本地变量,包和元数据联系起来。这样编译器可以将元数据存储在class文件中。这样虚拟机和其它对象可以根据这些元数据来决定如何使用这些程序元素或改变它们的行为。
二 定义一个简单的annotation并使用它
1.定义annotation
定义一个annotation是什么简单的,它采取的是类似于interface的定义方式: “@+annotation类型名称+(..逗号分割的name-value对...)”
| //example 1 package sz.starbex.bill.annotation; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target; import java.lang.annotation.elementtype; @retention(retentionpolicy.runtime) @target(elementtype.method) public @interface simpleannotation { string value(); } |
@retention这个meta-annotation表示我们创建的simpleannotation这个annotation将会存储在class文件中,并在java
vm运行时加载它。@target这个meta-annotation表示我们创建的simplwannotation将会为描述方法,而@interface simpleannotation是我们自定义的annotation,它有一个成员叫value,返回值是string。
2.使用annotation
| //example 2 package sz.starbex.bill.annotation; import sz.starbex.bill.annotation.simpleannotation; public class usingsimpleannotation { @simpleannotation(value="pass:this method will pass")//注意name=value的用法 public void pass(){ if(10>5) system.out.println("测试通过"); } @simpleannotation("fail:this method will fail")//注意name=value的用法 public void fail(){ if(10<5) system.out.println("测试失败"); } } |
一个annotation用于程序元素(在本例中是method),在method方法之前用(@annotation名称(name=value,name=value.....)。在本例中是@simpleannotation(value="pass:this method will pass")。每个annotation具有一个名字和成员个数>=0,当只有一个单一的成员时,这个成员就是value。我们也可以这样写 @simpleannotation("fail:this method will fail")。至此@simpleannotation将pass和fail联系起来了。
3.在运行时访问annotation
一旦annotation与程序元素联系起来,我们可以通过反射访问它们并可以取得它们的值。我们使用一个新的interface:java.lang.reflect.annotatedelement。java.lang.reflect.annotatedelement接口中的方法有:
- a. boolean isannotationpresent(class extends annotation> annotationtype)
如果指定类型的注释存在于此元素上,则返回 true,否则返回 false。 - b.
t getannotation(class annotationtype)
如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null。- c. annotation[] getannotations()
返回此元素上存在的所有注释。- d. annotation[] getdeclaredannotations()
返回直接存在于此元素上的所有注释。 - c. annotation[] getannotations()
你要注意 isannotationpresent和getannotation方法,它们使用了generics,请参考我的java 范型的blog。
下面我们列出一些实现了annotatedelement 接口的类
1. java.lang.reflect.accessibleobject
2. java.lang.class
3. java.lang.reflect.constructor
4. java.lang.reflect.field
5. java.lang.reflect.method
6. java.lang.package
下面的example程序说明了如何在运行环境访问annotation
| package sz.starbex.bill.annotation; import sz.starbex.bill.annotation.simpleannotation; import java.lang.reflect.method; public class simpleaccessannotation { static void accessannotationtest(class usingannnotationclass){ try { //object usingannnotationclass=class.forname(usingannotationclassname).newinstance(); method [] methods=usingannnotationclass.getdeclaredmethods();//取得对方法 for(method method:methods){ system.out.println(method.getname()); simpleannotation simpleannotation=method.getannotation(simpleannotation.class);//得到方法的annotation if(simpleannotation!=null){ system.out.print(simpleannotation.value()+"=="); string result=invoke(method,usingannnotationclass); system.out.println(result); } } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } static string invoke(method m, object o) { string result = "passed"; try { m.invoke(m,new object[]{}); } catch (exception e) { // todo auto-generated catch block result = "failed"; } return result; } /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub accessannotationtest(usingsimpleannotation.class); } } |
以上是简单的简绍annotation,让大家对annotation有一个初步的了解,下面二会简绍annotation的定义和语法。
java中的annotation解析之二
一、java 中的annotation的定义
java中的annotation
java定义了几个标准的meta-annotation,在新package中java.lang.annotation 中包含了以下meta-annotation:
meta-annotation 说明
@target 1. annotation的target是一个被标注的程序元素。target说明了annotation所修饰的对象范围:annotation可被用于packages、types(类、接口、枚举、annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在annotation类型的声明中使用了target可更加明晰其修饰的目标。
| meta-annotation | 说明 |
| @target | 1. annotation的target是一个被标注的程序元素。target说明了annotation所修饰的对象范围:annotation可被用于packages、types(类、接口、枚举、annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在annotation类型的声明中使用了target可更加明晰其修饰的目标。 2. elementtype的定义 type// class, interface, or enum (but not annotation) field// field (including enumerated values) |
| @retention | 1. source//按照规定使用注释,但是并不将它保留到编译后的类文件中 2. class//将注释保留在编译后的类文件中,但是在运行时忽略它 3. runtime//将注释保留在编译后的类文件中,并在第一次加载类时读取它 |
| @documented | documented 表示注释应该出现在类的 javadoc 中 |
| @inherited | 一个annotation将被继承 |
三个标准的annotation 在java.lang包中:
| @deprecated | 对不再使用的方法进行注释 |
| @override | 指明注释的方法覆盖超类的方法 |
| @suppresswarnings | 阻止编译器的警告,例:当类型不安全时 |
下例来说明这三个标准的annotation:
| package sz.starbex.bill.annotation; import java.util.arraylist; import java.util.list; public class simpleoverrideannotation { public static void main(string[] args) { simpleoverrideannotation test = new simpleoverrideannotation(); system.out.println(test.tostring()); } @override public string tostring() { return "自己的类自己输出"; } @deprecated public void dosomething() { system.out.println("方法已过时" ); } @suppresswarnings(value={"unchecked"}) public void testsuppresswarnings(){ list testlist=new arraylist(); testlist.add("kkkk");//没有使用范型,类型不安全 } } |
二、annotation使用实例
一个组合的annotation,注释类的
a. 商标annotation
| package sz.starbex.bill.annotation; public @interface trademark { string name(); string owner(); } |
b.license的annotation
| package sz.starbex.bill.annotation; import java.lang.annotation.*; @retention(retentionpolicy.runtime) @target({elementtype.type, elementtype.package}) public @interface license { string name(); string notice(); boolean redistributable(); trademark[] trademarks(); } |
c.测试类
| package sz.starbex.bill.annotation; @license(name="bill", notice="许可证", redistributable=true, trademarks={@trademark(name="mercedes",owner="swedish"), @trademark(name="daewoo",owner="korean") } ) public class testlicenseannotation { public static void main(string[] args) { testlicenseannotation test=new testlicenseannotation(); license license=test.getclass().getannotation(license.class); system.out.println("license发放人:"+license.name()); system.out.println("license注意事项:"+license.notice()); system.out.println("license许可:"+license.redistributable()); trademark [] marks=license.trademarks(); for(trademark mark:marks){ system.out.println("商标名称:"+mark.name()); system.out.println("商标的使用者:"+mark.owner()); } } } |
闽公网安备 35060202000074号