java自定义注解 利用反射获取注解属性的里的值

闲来无事,就研究下代码,只会照抄,也不明白其原理,东拼西凑,总算是可以用,纠结了好几次,看了好几次基础知识。

好了  不废话了,上代码

先创建注解类   

package com.cinsos.cinsos.MyUtils.Excel;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME )
public @interface Excel {
    /**
     * 导出到Excel中的名字.
     */
    public String name() default "";
    /**
     * 导出时在excel中每个列的高度 单位为字符
     */
    public double height() default 14;

    /**
     * 导出时在excel中每个列的宽 单位为字符
     */
    public double width() default 16;



}

再 创建注解字段所对应的实体类,也不确定没有什么什么用,就创建了一个

package com.cinsos.cinsos.MyUtils.Excel;

public class ExcelBean {




    public String getName() {
        return name;
    }

    public double getHeight() {
        return height;
    }

    public double getWidth() {
        return width;
    }

    @Override
    public String toString() {
        return "ExcelBean{" +
                "name='" + name + '\'' +
                ", height=" + height +
                ", width=" + width +
                '}';
    }

    public ExcelBean(String name, double height, double width) {
        this.name = name;
        this.height = height;
        this.width = width;
    }

    public String name;
    public double height;
    public double width;

    public void setName(String name) {
        this.name = name;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public void setWidth(double width) {
        this.width = width;
    }
}

再创建实体类来使用注解

package com.cinsos.cinsos.bean;

import com.cinsos.cinsos.MyUtils.Excel.Excel;
import java.util.Date;

public class User {
    public String getUser_oid() {
        return user_oid;
    }

    public String getUser_name() {
        return user_name;
    }

    public String getUser_uname() {
        return user_uname;
    }

    public String getUser_password() {
        return user_password;
    }

    public Integer getUser_type() {
        return user_type;
    }

    public String getUser_phone() {
        return user_phone;
    }

    public String getUser_email() {
        return user_email;
    }

    public Date getUser_greantime() {
        return user_greantime;
    }

    public void setUser_oid(String user_oid) {
        this.user_oid = user_oid;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public void setUser_uname(String user_uname) {
        this.user_uname = user_uname;
    }

    public void setUser_password(String user_password) {
        this.user_password = user_password;
    }

    public void setUser_type(Integer user_type) {
        this.user_type = user_type;
    }

    public void setUser_phone(String user_phone) {
        this.user_phone = user_phone;
    }

    public void setUser_email(String user_email) {
        this.user_email = user_email;
    }

    public void setUser_greantime(Date user_greantime) {
        this.user_greantime = user_greantime;
    }

    @Override
    public String toString() {
        return "User{" +
                "user_oid='" + user_oid + '\'' +
                ", user_name='" + user_name + '\'' +
                ", user_uname='" + user_uname + '\'' +
                ", user_password='" + user_password + '\'' +
                ", user_type=" + user_type +
                ", user_phone='" + user_phone + '\'' +
                ", user_email='" + user_email + '\'' +
                ", user_greantime=" + user_greantime +
                '}';
    }
    @Excel(name = "用户唯一编号")
    private String user_oid;
    @Excel(name = "姓名")
    private String user_name;
    @Excel(name = "账号")
    private String user_uname;
    @Excel(name = "密码")
    private String user_password;
    @Excel(name = "类型")
    private Integer user_type;
    @Excel(name = "手机号码")
    private String user_phone;
    @Excel(name = "邮箱")
    private String user_email;
    @Excel(name = "创建时间")
    private Date user_greantime;

}

再创建工具类来获取注解里的字段值

package com.cinsos.cinsos.MyUtils.Excel;
import com.cinsos.cinsos.bean.User;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class ExcelUtil <T>{
    /**
     * 实体对象
     */
    public Class<T> clazz;

    /**
     * 将实体类通过对象传入
     *
     */
    public ExcelUtil(Class<T> clazz)
    {
        this.clazz = clazz;
    }
    public  List<ExcelBean> getValue() throws Exception {
        //获取传入的类
        Class c=this.clazz;
        List<ExcelBean> list=new ArrayList<ExcelBean>();
        Field[] fields =c.getDeclaredFields();
        for(Field field:fields){
            if(field.isAnnotationPresent(Excel.class)){
                Excel D=field.getAnnotation(Excel.class);
                System.out.println(D.name()+D.height()+D.width());
                ExcelBean excelBean=new ExcelBean(D.name(),D.height(),D.width());
                list.add(excelBean);
            }
        }
       return list;
    }
   



}

然后再调用工具类,获取注解内容

完事,自己写的,代码写的很随意,大家可以抄抄已测试  没有什么问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值