注解

理解注解及应用
什么是注解?
注解是:
1)JDK1.5推出的一种新的应用类型(特殊的class)
2)元数据(Meta Data):一种描述性类型,用于描述对象.例如@Override
可以把注解理解为代码里的特殊标记,这些标记可以在编译,类加载,运行时被读取,并执行相应的处理。

注解的本质:描述元素(接口,类,注解,属性,参数,方法)
可以借助@interface关键字进行定义

案例1:
需求:doMethod01();获取GoodsMapper上的@Mapper的注解
需求:doMethod02();获取Gogods类上的Entity注解以及value属性值
获取Goods对象中的id属性上的ID注解及value属性

package com.java.annotation;
/**@interface 用于定义注解
 * @Target 用于描述注解,告诉编译器此注解可以描述哪些对象
 * @Retention 用于描述注解的有效范围
 */
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;

//定义Mapper注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Mapper{}

//定义Entity注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Entity{
	String value() default "";
}
//定义ID注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.METHOD})
@interface ID{
	String value() default "";
}

//定义一接口并使用注解修饰
@Mapper
interface GoodsMapper{}
//定义一个类并使用注解修饰
@Entity("goods")
class Goods{
	@ID("gid")
	Long id;
}
public class TestAnnotation01 {
	public static void main(String[] args) throws Exception {
		doMethod01();
		doMethod02();
		
	}

	private static void doMethod02() throws Exception{
		//获取Goods字节码对象
		Class<Goods> g =Goods.class;
		//获取Gogods类上的Entity注解以及value属性值
		Entity e = g.getAnnotation(Entity.class);
		System.out.println(e.value());
		//获取Goods对象中的id属性上的ID注解及value属性
		Field f=g.getDeclaredField("id");
		ID i =f.getAnnotation(ID.class);
		System.out.println(i.value());
	}
	//alt+shift+m
	private static void doMethod01() {
		//1,获取GoodsMapper的字节码对象
		Class<GoodsMapper> clazz = GoodsMapper.class;
		
		//2,获取GoodsMapper上的@Mapper的注解
		Mapper a=//Annotation
		clazz.getAnnotation(Mapper.class);
		System.out.println(a);
	}

	}

案例2:
需求:doMethod01();获取类上注解对应的属性值.
需求:doMethod02();显示包下所有 的class文件

package com.java.annotation;

import java.io.File;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.net.URL;


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Component{
	String value() default "";
	boolean lazy();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface ComponetScan{
	String value() default "";
}
@Component(value="searcheService",lazy=true)
class DefaultSearchService{}

@ComponetScan("com.java.cache")//需求:显示包下所有 的class文件
class ServiceConfig{
	
}
public class TestAnnotation02 {
	public static void main(String[] args) {
		doMethod01();
		doMethod02();
	}
	//加载ServiceConfig类并获取指定注解描述的特定包中的类
	public static void doMethod02() {
		//获取对象字节码
		Class<?> clazz = ServiceConfig.class;
		//获取注解
		ComponetScan cs = clazz.getAnnotation(ComponetScan.class);
		//获取包结构
		String pkg =cs.value();
		//将包路径的点转换成/
		String pkgPath = pkg.replace(".","/");
		//获取类加载系统资源路径,获取pkgPath对应的类的绝对路径
		URL url = ClassLoader.getSystemResource(pkgPath);
		System.out.println(url);
		//将路径传入文件对象
		File file = new File(url.getPath());
		//创建文件对象,呈现URL对应路径下的Class文件
		File[] files = file.listFiles();
		//遍历打印文件
		for(File f : files) {
			System.out.println(f.getName());
		}
	}
	 //需求:获取类上注解对应的属性值.
	private static void doMethod01() {
		//获取字节码对象
		Class<?> clazz =DefaultSearchService.class;
		//获取注解
		Component com =clazz.getAnnotation(Component.class);
		boolean falg =com.lazy();
		String value =com.value();
		System.out.println(falg);
		System.out.println(value);
	}

}

doMethod01():
输出:
true
searcheService
doMethod02():
输出:
file:/E:/framework/CGB-JAVASE-V1.2/target/classes/com/java/cache
Cache.class
FIFOCache.class
LruCache.class
PerpetualCache.class
Problem.class
SerializableCache.class
TestFIFOCache.class
TestSerializable.class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值