Java Introspector(内省)
来源:互联网 发布:广州网络推广gzdlwl 编辑:程序博客网 时间:2024/06/05 08:23
POJO
public class Student { // 创建属性(含有 get|set 的Field) private String name; private int age; private Date birthday; public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { //age return age; } public void setAge(int age) { this.age = age; } // 只有 set public int getAbc(){ //abc return 10; }}
内省
- Introspector构建一个BeanInfo对象。
- BeanInfo将一个类的所有属性进行了封装
- 获取属性描述器
PropertyDescriptor[] pds = bf.getPropertyDescriptors(); - PropertyDescriptor对象:
1. Method getReadMethod()
获得应该用于读取属性值的方法。
2. Method getWriteMethod()
获得应该用于写入属性值的方法。
public class 内省 { @Test public void test1() throws Exception { // 将Student中的所有属性封装到BeanInfo对象中 BeanInfo bf = Introspector.getBeanInfo(Student.class); // 属性描述器 PropertyDescriptor[] pds = bf.getPropertyDescriptors(); // System.out.println(pds.length); for (PropertyDescriptor pd : pds) { System.out.println(pd.getName()); } /* 5 abc age birthday class name */ } @Test public void test2() throws Exception { Student stu = new Student(); // 得到指定的属性对象 PropertyDescriptor pd = new PropertyDescriptor("name", Student.class); Method setter = pd.getWriteMethod();// 得到setter方法:setName() setter.invoke(stu, "tom"); Method getter = pd.getReadMethod(); // 得到getter方法:getName() System.out.println(getter.invoke(stu, null)); } @Test public void test3() throws Exception { Student stu = new Student(); // 得到指定的属性对象 PropertyDescriptor pd = new PropertyDescriptor("age", Student.class); Method setter = pd.getWriteMethod();// 得到setter方法:setName() setter.invoke(stu, 18); Method getter = pd.getReadMethod(); // 得到getter方法:getName() System.out.println(getter.invoke(stu, null)); }}
BeanUtils 的封装使用
public class BeanUtil { @Test public void test1() throws Exception{ Student stu = new Student(); BeanUtils.setProperty(stu, "name", "张三"); //给属性赋值 String s = BeanUtils.getProperty(stu, "name"); System.out.println(s); } @Test public void test2() throws Exception{ Student stu = new Student(); //BeanUtils默认支持8种基本数据类型,自动转换 BeanUtils.setProperty(stu, "age", "18"); //给属性赋值 String s = BeanUtils.getProperty(stu, "age"); System.out.println(s); } @Test public void test3() throws Exception{ Student stu = new Student(); //注册类型转换器 ConvertUtils.register(new DateLocaleConverter(), Date.class); BeanUtils.setProperty(stu, "birthday", "1990-11-12"); //给属性赋值 String s = BeanUtils.getProperty(stu, "birthday"); System.out.println(s); } @Test public void test4() throws Exception{ Student stu = new Student(); //注册类型转换器 ConvertUtils.register(new Converter() { public Object convert(Class type, Object value) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); if(value instanceof String){ String v = (String) value; try { return sdf.parse(v); } catch (ParseException e) { throw new RuntimeException(e); } } return null; } }, Date.class); BeanUtils.setProperty(stu, "birthday", "1995-11-12"); //给属性赋值 String s = BeanUtils.getProperty(stu, "birthday"); System.out.println(s); } @Test public void test5() throws Exception{ // Map m = new HashMap(); m.put("name", "张三"); //key名一定要与对象中的变量名一致 m.put("age", "18"); //key名一定要与对象中的变量名一致 m.put("birthday", "1992-05-12"); //key名一定要与对象中的变量名一致 Student stu = new Student(); ConvertUtils.register(new DateLocaleConverter(), Date.class); BeanUtils.populate(stu, m); //将Map属性自动放到Bean中 System.out.println(stu.getName()); }}
阅读全文
0 0
- Java Introspector(内省)
- java内省(Introspector)简介
- Java的内省(introspector)
- java中的内省(Introspector)
- Java web-内省(Introspector)
- java内省(Introspector)简介及实例
- Java基础加强之内省(introspector)
- Java基础加强之内省(introspector)
- Java学习笔记-JavaBean-内省(Introspector)
- Java的内省(Introspector)操作
- Java--introspector(内省)操作bean属性
- Java 内省技术(Introspector)
- JAVA内省 Introspector
- java (内省)Introspector
- Java内省IntroSpector应用
- JAVA内省(Introspector)
- Java内省IntroSpector应用
- Java内省IntroSpector应用
- Android开发中常见的颜色
- 【Java报错】java.text.ParseException: Unparseable date
- jdk7和8的一些新特性介绍
- ora-01654 unable to extend index
- Spring boot 服务与配置
- Java Introspector(内省)
- 第十周项目一
- 打印倒三角和菱形
- 第九周项目3
- compile freeswitch based on macOS
- Android播放音频
- Linux基础之八
- rest/flask/nginx/uwsgi/supervisor
- 常用的距离测度