自定义的annotation 使用方法

本文介绍如何在Java中创建并使用自定义注解,包括针对类和字段的注解定义及其实现方式。通过具体示例展示了如何读取和设置注解值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天心情真心不爽,发现公司居然这么瞧不起人。唉。挥泪啊。。。换工作

   总结下前几天做了个实例 自定义annotation的用法,这个完全可以更新我们之前提到封装的ldap基层的map。

   首先先上个代码,先创建annotation

1
2
3
4
5
6
7
8
9
10
11
12
13
package  com.annotation.pengbo.annotation;
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;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public  @ interface  UserAttribute {
     public  String  value()  default  "" ;
     public  String  name()  default  "" ;
}


上面三个@属性就不多说了 , 里面定义了2个可以赋值的属性一个是value也就是默认值,另一个就是name赋值时必须写成{name=""}。

这个是给一般类里的属性使用的。

下面在写一个给类名使用的

1
2
3
4
5
6
7
8
9
10
11
12
package  com.annotation.pengbo.annotation;
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;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public  @ interface  UserClass {
     public  String  value()   default  "" ;
}


不解释了,下面在写一个实体 来演示下 这2个注解怎么用了

1
2
3
4
5
6
7
8
9
10
package  com.annotation.pengbo.model;
import  com.annotation.pengbo.annotation.UserAttribute;
import  com.annotation.pengbo.annotation.UserClass;
@UserClass( "user" )
public  class  User {
     @UserAttribute(name= "uu" )
     private  String  uuid;
     @UserAttribute
     private  String  name;
//get、set省略...


设定他们属性对应的值。


接下来看看怎么使用。

下面是获得自定义注解和注解中的值。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public  void  explainAnnotation(T t) throws Exception {
     Class<?> entity =t.getClass();
            //获取到UserClass的注解
     UserClass annotation = entity.getAnnotation(UserClass. class );
            //输出注解中的value值
     System.out.println(annotation.value());
    //输出T(实体)类的名字
     System.out.println(entity.getName());
     for ( Field f : entity.getDeclaredFields() ) {
                    //设置获得属性的权限
         f.setAccessible( true );
                    //获得这个实体中所有的UserAttribute的注解
         UserAttribute s =(UserAttribute) f.getDeclaredAnnotations()[ 0 ];
                    //输出注解的名字
         System.out.println(s.name());
                //输出实体的属性名字
            System.out.println(f.getName());
                //获得实体属性的值
            Object  fieldVal = f. get (t);
                //输出出来
            System.out.println(fieldVal);
     }
                                                              
}


下面的方法是如何将值赋值到实体中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
          public  <T> T setAnnotation(Class<?> entity) throws Exception {
//           Class<?> entity =t.getClass();
//          UserClass annotation = entity.getAnnotation(UserClass.class);
                 //获得传入的实体类名
              String  fieldName = entity.getName();
              System.out.println(fieldName);
                 //实例化它
             T t= (T) entity.newInstance();
              for ( Field f : entity.getDeclaredFields() ) {
                 //给它权限(如果不给值能对实体中属性公开的注解操作,例如public)
                                  f.setAccessible( true );
                                 //将你想赋的值,设置进去
                     f. set (t,  "hello uuuu" );
                     System.out.println(f.getName());
              }
                         //返回它
             return  t;
          }


前面几段代码 没看懂无所谓,遍地都是,可以随便翻翻 找找很多解释如何去建立注解,重点是后面两个,如何去获取和设置。这只是一个简单的操作,具体的可以查考api提供的方法

本文出自 “记下就对了” 博客,请务必保留此出处http://jueshizhanhun.blog.51cto.com/4372226/1284304

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值