package com.bingo.frame.oop.serial;
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;
import org.apache.catalina.User;
/**
* 获取自定义注解属性值
* @author bingo
*/
@Retention(RetentionPolicy.RUNTIME)//运行时有效
@Target(ElementType.METHOD)//修饰方法
@interface Select{
String value() default "select * from user_info";
}
interface UserInfoDao {
@Select
User getUsers();
}
public class TestAnnotation {
public static void main(String[] args) throws Exception{
//1.获取UserInfoDao接口字节码对象
Class cls = UserInfoDao.class;
//2.获取接口中getUsers方法对象
Method method = cls.getDeclaredMethod("getUsers", null);
//3.获取方法上的@Select注解对象
Select annotation = method.getAnnotation(Select.class);
//4.获取@Select注解对象中的value
String value = annotation.value();
System.out.println(value);//select * from user_info
}
}
获取自定义注解属性值
最新推荐文章于 2023-07-09 22:11:20 发布