---
title: "JAVA Annotation"
author: "threeman"
date: "2014-07-26T20:36:00Z"
tags: []
categories: ["编程语言"]
slug: "java-annotation"
draft: false
lastmod: "2014-07-27T09:57:22Z"
id: 2096812
url: "https://threeman.iteye.com/blog/2096812"
---

介绍

尤其是我们在学习Spring时，都绕不开Annotation，而且使用的非常频繁，并且给我们带来很大的便利， 所以我们有必要了解JAVA Annotation。

在此，我们自定义两个Annotation：一个是Class相关的Annotation；另一个是Method相关的Annotation。然后，我们写一个类来使用那两个Annotation，这样我们就能比较快速的了解Annotation。

 

自定义一个Class相关的Annotation：Message.java

 

```
package shuai.study.annotation;

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

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Message {
	String mess();
}
```

\-------------------------------------------------------------------------------------------------------------------------------------

对于上面的Annotation，你可能不清楚@Target(ElementType.TYPE) 和@Retention(RetentionPolicy.RUNTIME) 是什么意思，那么我们就可以看一下JDK的源码：

 

```
public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,

    /** Field declaration (includes enum constants) */
    FIELD,

    /** Method declaration */
    METHOD,

    /** Parameter declaration */
    PARAMETER,

    /** Constructor declaration */
    CONSTRUCTOR,

    /** Local variable declaration */
    LOCAL_VARIABLE,

    /** Annotation type declaration */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE
}
```

 

```
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}
```

 

 

JDK的源码就是NB，写得非常清楚！

\-------------------------------------------------------------------------------------------------------------------------------------

 

另一个Method相关的Annotation：Employee.java

 

```
package shuai.study.annotation;

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

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Employee {
	String name() default "XXX";

	int budget() default 0;
}
```

 

再写一个类Company.java，来使用上面的两个Annotation：

 

```
package shuai.study.annotation;

@Message(mess = "NB company publish month start, please pay attention to this message")
public class Company {
	@Employee(name = "SB", budget = 6666)
	public void monthStar() {
		System.out.println("向钱看，向厚赚！");
	}
}
```

最后再来一个测试启动类AnnotationTest.java：

 

 

```
package shuai.study.annotation;

import java.lang.reflect.Method;

public class AnnotationTest {

	public static void main(String[] args) throws Exception {
		Class<?> companyClass = Class.forName("shuai.study.annotation.Company");

		if (companyClass.isAnnotationPresent(Message.class)) {
			Message message = companyClass.getAnnotation(Message.class);
			System.out.println("=== " + message.mess() + " ===");
		}

		Method[] methods = companyClass.getMethods();
		int length = methods.length;

		for (int index = 0; index < length; index++) {
			if (methods[index].isAnnotationPresent(Employee.class)) {
				Employee employee = methods[index].getAnnotation(Employee.class);
				System.out.println("Employee Name: " + employee.name());
				System.out.println("Employee Budget: " + employee.budget());
			}
		}
	}
}
```