package com.cy.java.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
//自定义注解:借助@interface定义
@Retention(RetentionPolicy.RUNTIME)//描述注解在运行期间有效
@Target(ElementType.METHOD)//描述注解修饰哪类成员
@interface Delete{
String value() default "";//属性
}
//商品数据层接口
interface GoodsDao{
@Delete("delete from db_10")
int deleteById();
}
//测试类
public class TestAnnotation {
public static void main(String[] args) throws Exception {
//业务:基于反射技术获取GoodsDao接口方法deleteById的注解内容
//反射的入口对象是谁?字节码对象
//1.获取接口字节码对象
Class<?> class1=GoodsDao.class;
//2.获取对象的方法
Method method = class1.getMethod("deleteById");
//3.获取方法上的注解
Delete annotation = method.getAnnotation(Delete.class);
//4.获取注解的内容
String value = annotation.value();
System.out.println(value);
}
}
//delete from db_10
异常:
分析: