自定义注解的使用

自从入行到现在,这个东西除了学习见过,基本没自己用过,最近在做一个保险电商项目。特此巩固一下知识点
https://blog.csdn.net/zhangbeizhen18/article/details/87885441

自定义注解的使用

场景:为了理解@interface使用
1.@interface自定义注解
<1>@interface自定义注解自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节。
<2>在定义注解时,不能继承其他的注解或接口。
<3>使用@interface来声明一个注解,
1>.每一个方法实际上是声明了一个配置参数,
2>.方法的名称就是参数的名称,
3>.返回值类型就是参数的类型,(返回值类型只能是基本类型、ClassStringenum)
4>.可以通过default来声明参数的默认值
2.举例说明,分别作用在类,方法,属性上的注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FiledAnnotation {
	 String value() default "GetFiledAnnotation";   
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodAnnotation {
	String name() default "MethodAnnotation";   
    String url() default "https://www.cnblogs.com";
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TypeAnnotation {
	
	String value() default "Is-TypeAnnotation";
	
}
@TypeAnnotation(value = "doWork")
public class Worker {
 
	@FiledAnnotation(value = "CSDN博客")
	private String myfield = "";
 
	@MethodAnnotation()
	public String getDefaultInfo() {
		return "do the getDefaultInfo method";
	}
 
	@MethodAnnotation(name = "百度", url = "www.baidu.com")
	public String getDefineInfo() {
		return "do the getDefineInfo method";
	}
}
public class TestMain {
	
	public static void main(String[] args) throws Exception {
		
        Class cls = Class.forName("com.zbz.annotation.pattern3.Worker");
        Method[] method = cls.getMethods();
        /**判断Worker类上是否有TypeAnnotation注解*/
        boolean flag = cls.isAnnotationPresent(TypeAnnotation.class);
        /**获取Worker类上是TypeAnnotation注解值*/
        if (flag) {
        	TypeAnnotation typeAnno = (TypeAnnotation) cls.getAnnotation(TypeAnnotation.class);
        	System.out.println("@TypeAnnotation值:" + typeAnno.value());
        }
        
        /**方法上注解*/
        List<Method> list = new ArrayList<Method>();
        for (int i = 0; i < method.length; i++) {
            list.add(method[i]);
        }
        
        for (Method m : list) {
        	MethodAnnotation methodAnno = m.getAnnotation(MethodAnnotation.class);
            if (methodAnno == null)
                continue;
            System.out.println( "方法名称:" + m.getName());
            System.out.println("方法上注解name = " + methodAnno.name());
            System.out.println("方法上注解url = " + methodAnno.url());
        }
        /**属性上注解*/
        List<Field> fieldList = new ArrayList<Field>();
        for (Field f : cls.getDeclaredFields()) {// 访问所有字段
        	FiledAnnotation filedAno = f.getAnnotation(FiledAnnotation.class);
        	System.out.println( "属性名称:" + f.getName());
        	System.out.println("属性注解值FiledAnnotation = " + filedAno.value());
        } 
	}
 
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值