java注解

Show The Code

 

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;

/**
 * 定义类级别的注解
 * @author zy
 *
 */
@Target(ElementType.TYPE) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
public @interface Description { 
	String value(); 
} 

 

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;

/**
 * 方法级别的注解<br/>
 * ElementType的类型在对应的枚举类中描述
 * 
 * @author zy
 *
 */
@Target(ElementType.METHOD) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
public @interface Name { 
String originate(); 
String community(); 
} 

 

/**
 * 注解使用示例
 * @author zy
 *
 */
@Description(value = "javaeye,做最棒的软件开发交流社区")
public class JavaEyer {
	@Name(originate = "创始人:robbin", community = "javaEye")
	public String getName() {
		return null;
	}

	@Name(originate = "创始人:江南白衣", community = "springside")
	public String getName2() {
		return "借用两位的id一用,写这一个例子,请见谅!";
	}
}

 

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
 * 注解解析类的例子,一般在框架中实现对业务调用的解析。
 * <br/>
 * 使用反射解析
 * @author zy
 *
 */
public class TestAnnotation {
	/**
	 * author lighter 说明:具体关天Annotation的API的用法请参见javaDoc文档
	 */
	public static void main(String[] args) throws Exception {
		String CLASS_NAME = "com.buyaoji.annotation.JavaEyer";
		Class test = Class.forName(CLASS_NAME);
		Method[] method = test.getMethods();
		boolean flag = test.isAnnotationPresent(Description.class);
		if (flag) {
			Description des = (Description) test
					.getAnnotation(Description.class);
			System.out.println("描述:" + des.value());
			System.out.println("-----------------");
		}
		// 把JavaEyer这一类有利用到@Name的全部方法保存到Set中去
		Set<Method> set = new HashSet<Method>();
		for (int i = 0; i < method.length; i++) {
			boolean otherFlag = method[i].isAnnotationPresent(Name.class);
			if (otherFlag)
				set.add(method[i]);
		}
		for (Method m : set) {
			Name name = m.getAnnotation(Name.class);
			System.out.println(name.originate());
			System.out.println("创建的社区:" + name.community());
		}
	}
}

 

 

运行结果:

描述:javaeye,做最棒的软件开发交流社区
-----------------
创始人:robbin
创建的社区:javaEye
创始人:江南白衣
创建的社区:springside

 

 

总结

注解的使用方法:

1、自定义注解,注意注解的时空范围,简单说就是注解针对的目标(类、方法、字段),以及注解的时效(运行时、或者源码中有效)。
2、要获取注解的信息,必须通过Java的反射技术来获取Annotation对象,因为你除此之外没有别的获取注解对象的方法。
3、获取了注解对象,就可以调用注解的方法来获取相对应的值了。为基础框架所用。
4、当然,注解也可以没有定义成员,这样注解就成了一个标记符号了。

 

注解在框架(Junit,Spring)中的使用:

1、注解的功能,使用java代码也能实现,但使用注解提供了一种简洁性。同时省去了框架与业务类间的桥接代码,利用注解和反射的天生功能,优雅的将业务对象(包括对应的值)提供给框架。

2、注解在spring中的应用待补充。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值