java-内省技术解析
来源:互联网 发布:java web项目规范 编辑:程序博客网 时间:2024/06/08 11:42
1 javabean的软件设计思想
2 内省:封装了java反射,提供直接操作属性的Setter和getter方法的方法
3 核心API:BeanInfo java 的描述信息,Introspector 内省的变成入口,PropertyDescriptor 属性的描述器
1 javaBean 简介
内省(Insrospector)
javabean 是一中java类的设计思想,对普通java类的设计提供了一些约定,比如,所有的字段都应该是private私有的,应该为所有的字段提供setter和getter方法来操作这些字段,至少有一个无参构造函数(创建对象的newInstance),不可以直接访问一个类的私有字段,要访问,使用setter和getter
//符合javaBean设计思想的普通java类
public class Student{ private String id; //id字段是私有的 private String name ;//name字段是私有的 public Student(){} //提供一个无参数构造函数 public String getId(){} //获得id字段的值 public void setId(String id){} // 设置id字段的值 public String getName(){} //获得name字段的值 public void setName(String name){} //设置name 字段的值}
2)如何使用数据封装Student对象
存储了数据的Map对象,把这个Map对象中的数据封装成Student对象
Map<String ,Object > map=new HashMap<String ,Object >();map.put("id","10000000");map.put("name","建国");map.put("age",30);
三种方式:
1.直接调用Student对象的响应的setter方法
使用反射技术
使用内省技术
2.使用反射技术封装对象
3.内省API以及使用内省封装对象
java 内省封装了java反射
1Java 内省依赖javabean设计思想和java反射,内省要求java类符合javabean的设计思想
2.java内省使用java反射解析java类的结构,把字段和该字段对象的setter和getter方法关联起来,封装到PropertyDescriptor对象中,称为属性描述器,通过属性描述器可以直接获得该字段的setter和getter方法的Method对象
3 使用内省的编程步骤
//获得beanInfo信息,Introspector,propertyDescriptorsBeanInfo beaninfo=Introspector.getBeanInfo(obj.getClass());PropertyDescriptor[] propertyDescriptors=beanInfo.getPropertyDescriptors();for(PropertyDescriptor propertyDescriptor: propertyDescriptors ){String fileName=propertyDescriptor.getName();//获得Method对象之后,就可以按照反射的做法调用方法了Method setMethod=propertyDescriptor.getWriteMethod();setMethod.invoke(obj,value);}
BeanInfo
BeanInfo 是一个符合JavaBean规范的Java类的描述信息,可以通过BeanInfo对象获得所有字段的描述其PropertyDescriptor(包含字段信息和该字段的setter和getter方法)
核心方法: PropertyDescriptor[] getPropertyDescriptors()
BeanInfo的对象通过
BeanInfo beanInfo =Introspector.getBeanInfo(clazz) ;方法获得
Introspector
Introspector 内省,能够解析java类生成该类的BeanInfo信息的对象
Introspector 类是java内省的编程入口,一般都是通过Introspector类解析一个Class对象生成一个BeanInfo对象
BeanInfo beanInfo=Introspector.getBeanInfo(obj.getClass());
getBeanInfo 内部是通过java反射技术来解析一个类
Introspector只是提供了一个解析类的功能,并没有其他东西
PropertyDescriptor
属性描述器包含一个字段的名称,类型,包括此字段的setter和getter方法
核心方法:
Class<?> getPropertyType() //获得字段的类型Method getReadMethod() //获得字段的读方法(getter)Method getWritterMethod() //获得字段的写方法(setter)String getName() //获得字段的名字
java反射和java内省
java反射机制就是通过解析类的内部结构,获得这个类的所有属性和方法,并且可以操作这些属性和调用这些方法
反射技术除了可以使我们开发通用的功能外,还可以让程序在运行时加载并使用最开始不存在的新类
java内省专门用给javabean 服务的,方便把数据封装到对象中
反射和内省的特点
4.动手写ORM
下面是一个内省实例:
package cn.edu.gzu.utils;import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.Method;public class BeanUtils { //参数1 bean对象 //参数2 要获得Bean对象对应属性名称 public static Method getWriteMethod(Object beanObj, String name) { Method method = null; //使用内省技术来实现该方法 //1 分析Bean对象=》BeanInfo try { BeanInfo info = Introspector.getBeanInfo(beanObj.getClass()); //2 根据BeanInfo获得所有属性的描述器 PropertyDescriptor[] pds = info.getPropertyDescriptors(); //3 遍历这些属性描述器 if(pds != null){ for(PropertyDescriptor pd : pds){ //判断当前遍历的描述器的属性是否是我们要找的属性 //获得当前描述器的属性名称 String pName = pd.getName(); //使用要找的属性名称与当前描述器的属性名称比对 if(pName.equals(name)){ //比对已知=》 找到了 获得写入属性的set方法 method = pd.getWriteMethod(); } } } //判断当前遍历的描述的属性是否是我们要找的属性 //4 返回找到的set方法 } catch (IntrospectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } //如果没有找到=》抛出异常提示用户检测是否创建属性对应的set方法 if(method == null){ throw new RuntimeException("please check "+name+"'s set methods"); } return method; }}
- java-内省技术解析
- Java的内省技术
- Java 内省技术(Introspector)
- java之内省技术
- Java内省技术
- Java内省技术
- 内省技术
- Java中的反射与内省技术内幕
- JAVA 内省
- java 内省
- java 内省
- Java内省
- java内省
- java内省
- java--内省
- Java内省
- java内省
- Java内省
- 刷题总结
- windows 控制台 bat 注册service服务
- 从0开始学习Git系列之「Git安装」
- Error:(2, 0) No service of type Factory<LoggingManagerInternal> available in ProjectScopeServices. <
- Windows加载DLL
- java-内省技术解析
- 【Android实战之旅 003】Android UI模板设计
- 从0开始学习 GitHub 系列之「向GitHub 提交代码」
- 括号配对算法
- 第九章:菱形
- 从0开始学习Git系列之「Git简介」
- js单选按钮动画特效
- c++获取注册表中程序的安装路径
- 从0开始学习Git系列之「Git初阶」