JAVA Annotation

介绍

    尤其是我们在学习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 star, 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());
			}
		}
	}
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值