java反射,通過自定義註解獲取get方法

1.新建一個類配置自定義註解

/******
 * @Target設置註解可用範圍;
 * @Retention設置什麼使用啓用註解
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Config {
    /***
     * isExport () 是否導出默認爲false
     * Clumindex ()設置順序,默認值爲0
     */
    boolean isExport () default false;
    int Clumindex () default 0;
}

2.新建一個數據實體並給所需要的方法設註解

public class StudentEntity {
    private Long pid;
    private String name;
    private String info;
    private Long create_by;
    private Long update_by;
    private Boolean deleted;
    private Long version;
    private Map<String,Object> ext;
    private Date create_time;
    private Date update_time;
    private String stu_num;
    private Long sex;
    private String email;
    public String phone;
    public Map<String, Object> getExt() {
        return ext;
    }

    public void setExt(Map<String, Object> ext) {
        this.ext = ext;
    }

    public Date getCreate_time() {
        return create_time;
    }

    public void setCreate_time(Date create_time) {
        this.create_time = create_time;
    }

    public Date getUpdate_time() {
        return update_time;
    }

    public void setUpdate_time(Date update_time) {
        this.update_time = update_time;
    }

    public String getStu_num() {
        return stu_num;
    }

    public void setStu_num(String stu_num) {
        this.stu_num = stu_num;
    }

    public Long getSex() {
        return sex;
    }

    public void setSex(Long sex) {
        this.sex = sex;
    }
    @Config(isExport = true,Clumindex = 1)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    @Config(isExport = true,Clumindex = 2)
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }



    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }
    @Config(isExport = true,Clumindex = 3)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @Config(isExport = true,Clumindex = 1)
    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public Long getCreate_by() {
        return create_by;
    }

    public void setCreate_by(Long create_by) {
        this.create_by = create_by;
    }

    public Long getUpdate_by() {
        return update_by;
    }

    public void setUpdate_by(Long update_by) {
        this.update_by = update_by;
    }

    public Boolean getDeleted() {
        return deleted;
    }

    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }
    @Override
    public String toString() {
        return "StudentEntity{" +
                "pid=" + pid +
                ", name='" + name + '\'' +
                ", info='" + info + '\'' +
                ", create_by=" + create_by +
                ", update_by=" + update_by +
                ", deleted=" + deleted +
                ", version=" + version +
                ", ext=" + ext +
                ", create_time=" + create_time +
                ", update_time=" + update_time +
                ", stu_num='" + stu_num + '\'' +
                ", sex=" + sex +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

3.測試

public class test {


    public static void main(String[] arg) {
        fanshe1();
    }
    public static void fanshe1() {
        Class clazz = null;
        try {
            //設置需要反射的類(含包名)
            clazz = Class.forName("com.wangruoyao.springbootdemo.model.StudentEntity");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //獲取類下面的所有方法
         Method[] methods =clazz.getDeclaredMethods();
        //定義一個map用來存儲方法:Integer是自己設置的順序
        Map<Integer,Method> methodMap =new HashMap<>();
         //遍歷Method[]
        for(Method m:methods){
            System.out.println(m);
            //判斷是否是註解
            if(m.isAnnotationPresent(Config.class)){
              Config arr=m.getAnnotation(Config.class);
              //判斷是否需要導出
              if(arr.isExport()){
                  //把篩選後的方法放入Map中
                  methodMap.put(arr.Clumindex(),m);
              }
            }

        }
        /********
         * 通過entrySet遍歷Map(適用於大量)
         */
        for(Map.Entry<Integer,Method> entry :methodMap.entrySet()){
            System.out.println("+++++++++++++++key="+entry.getKey()+"value="+entry.getValue());

        }
        /********
         * 通過keySet遍歷Map
         */
//        for(Integer integer:methodMap.keySet()){
//            Method m=methodMap.get(integer);
//            System.out.println(integer+"+++++++++++++value="+m);
//        }

    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章