Spring框架学习入门反射

来源:互联网 发布:数学之美 知乎 编辑:程序博客网 时间:2024/06/04 19:28

对框架我们充满了好奇,感觉很神秘今天我就以我的一些经验给大家展示框架的一些基础知识

1,反射

自定义一个类

package cn.itcast.shujujiegou.StructuresAnalysis;/** * Created by likailong on 2016/9/29. */public class Node {    private String name;    private String email;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }}
现在我们用java提供的一些类来得到这个类的一些方法

package cn.itcast.shujujiegou.StructuresAnalysis;import java.beans.IntrospectionException;import java.beans.PropertyDescriptor;import java.lang.reflect.Field;/** * Created by likailong on 2016/9/29. */public class PropertiesDescriptionSample {    public static void main(String [] args) throws IntrospectionException {        Field[] fileds = Node.class.getDeclaredFields();        for(Field filed:fileds){            System.out.println("=========="+filed.getName()+"\t"+filed.getType());            PropertyDescriptor proper=new PropertyDescriptor(filed.getName(),Node.class);            printDesc(proper);        }    }    private static void printDesc(PropertyDescriptor proper) {        System.out.println(proper.getDisplayName());        System.out.println(proper.getShortDescription());        System.out.println(proper.getPropertyEditorClass());        System.out.println(proper.getPropertyType());        System.out.println(proper.getReadMethod());        System.out.println(proper.getWriteMethod());    }}
结果如下


得到了类的所有信息。





0 0