当前页面: 开发资料首页 → Java 专题 → jdk1.5中的元数据一例
jdk1.5中的元数据一例
摘要: jdk1.5中的元数据一例
</td>
</tr>
<tr>
<td height="35" valign="top" class="ArticleTeitle">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="265" height="265" align="center" valign="top">
</td>
<td width="57%" valign="top">
元数据,在java中也叫注释、注解。微软的.net从开始设计时就有这个功能,不过它的术语叫属性。
这是一个强大的功能,程序员如果想挣钱,得好好研究它,因为
在将来的j2ee开发中,广泛的使用它,包括ejb的声明,IOC中的注入等。
IBM网站有篇文章详细介绍了它,挺好的一篇文章。
http://www-900.ibm.com/developerWorks/cn/java/j-annotate1/
http://www-900.ibm.com/developerworks/cn/java/j-annotate2.shtml
</td>
</tr>
<tr>
<td height="44" colspan="2">
新建一个注释类型,这个类型指明了一本名著的作者和他的email。
-
- package com.kuaff.jdk5;
-
-
-
- import java.lang.annotation.*;
-
-
-
- @Retention(RetentionPolicy.RUNTIME)
-
- @Target(ElementType.METHOD)
-
- public @interface BookAuthor
-
- {
-
- String name();
-
- String email();
-
- }
-
-
-
下面使用这个注释,为我们的方法加上注解:
-
- package com.kuaff.jdk5;
-
-
-
- import java.lang.annotation.Annotation;
-
-
-
- public class MetadataShow
-
- {
-
- @BookAuthor(name="曹雪芹",email="caoxueqin@hongloumeng.books")
-
- public void introHongLouMeng()
-
- {
-
- System.out.println("这是一本好书啊");
-
- }
-
-
-
- public static void main(String[] args)
-
- {
-
- MetadataShow metadata = new MetadataShow();
-
- try
-
- {
-
- Annotation[] annotation = metadata.getClass().getMethod("introHongLouMeng").getAnnotations();
-
- for(Annotation a : annotation)
-
- {
-
- System.out.printf("作者:%s%n", ((BookAuthor)a).name());
-
- System.out.printf("他的电子邮件(可能已被注销):%s%n", ((BookAuthor)a).email());
-
- }
-
- }
-
- catch (SecurityException e)
-
- {
-
- e.printStackTrace();
-
- }
-
- catch (NoSuchMethodException e)
-
- {
-
- e.printStackTrace();
-
- }
-
- }
-
- }
-
-
-
请注意,要想在程序运行时能读取这些注释,需要在注释的声明的时候加上
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) //也可能时其他类型,如针对声明的注释
这是对注释的注释。
编译运行这两个文件:
C:\java>java MetadataShow
作者:曹雪芹
他的电子邮件(可能已被注销):caoxueqin@hongloumeng.books
C:\java>
</td>
</tr>
</table>
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
</td>
</tr>
<tr>
↑返回目录
前一篇: java实现的简单的端口扫描程序
后一篇: 我也用字典探表单密码