java内省Introspector
大纲:
- JavaBean 规范
- 内省
一、JavaBean 规范
JavaBean —般需遵循以下规范。
- 实现 java.io.Serializable 接口。
- javaBean属性是具有getter/setter方法的private成员变量。也可以只提供getter方法,这样的属性叫只读属性;也可以只提供setter方法,这样的属性叫只写属性。
- JavaBean 必须有一个空的构造函数
- JavaBean 必须有一个public构造函数
二、内省
- 首先内省的本质也是利用反射
- 通过内省可以快速拿到javaBean变量的getter与setter,从而快速操作bean对象。
测试用javaBean
public class Person {
private String name ;
private int age ;
public void say(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
1.可以通过BeanInfo对象拿到JavaBean类中所有属性和方法的描述器
public static void main(String[] args) throws Exception {
//获取BeanInfo对象,主要用于获取javaBean的属性描述器
BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
//方法描述器
MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
//属性描述器
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
//测试对象
Person person = new Person();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
//属性名
System.out.println("Person的属性:"+propertyDescriptor.getName());
if("name".equals(propertyDescriptor.getName())){
//属性读方法
Method readMethod = propertyDescriptor.getReadMethod();
//属性的写方法
Method writeMethod = propertyDescriptor.getWriteMethod();
writeMethod.invoke(person,"xiaoming");
System.out.println("person的name:"+readMethod.invoke(person));
Class<?> propertyType = propertyDescriptor.getPropertyType();
System.out.println("属性的类型:"+propertyType.getName());
}
}
}
2.直接获取某个属性的描述器
public static void main(String[] args) throws Exception {
Person p = new Person();
//直接通过名称获取属性描述器
//获取name属性的描述器,方便我们直接操作p对象的name属性。
PropertyDescriptor descriptor = new PropertyDescriptor("name", Person.class);
Method writeMethod = descriptor.getWriteMethod();
writeMethod.invoke(p,"nananan");
System.out.println("person对象name:"+descriptor.getReadMethod().invoke(p));
}
java内省Introspector的更多相关文章
- Java 内省(Introspector)深入理解
Java 内省(Introspector)深入理解 一些概念: 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类 ...
- Java 内省(Introspector)和 BeanUtils
人生若只如初见,何事秋风悲画扇. 概述 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传递数据信息, ...
- JAVA内省(Introspector)
什么是Java内省:内省是Java语言对Bean类属性.事件的一种缺省处理方法. Java内省的作用:一般在开发框架时,当需要操作一个JavaBean时,如果一直用反射来操作,显得很麻烦:所以sun公 ...
- 聊聊Java内省Introspector
前提 这篇文章主要分析一下Introspector(内省,应该读xing第三声,没有找到很好的翻译,下文暂且这样称呼)的用法.Introspector是一个专门处理JavaBean的工具类,用来获取J ...
- Java 内省 Introspector
操纵类的属性,有两种方法 反射 内省 面向对象的编程中,对于用户提交过来的数据,要封装成一个javaBean,也就是对象 其中Bean的属性不是由字段来决定的,而是由get和Set方法来决定的 pub ...
- 【小家Spring】Spring IoC是如何使用BeanWrapper和Java内省结合起来给Bean属性赋值的
#### 每篇一句 > 具备了技术深度,遇到问题可以快速定位并从根本上解决.有了技术深度之后,学习其它技术可以更快,再深入其它技术也就不会害怕 #### 相关阅读 [[小家Spring]聊聊Sp ...
- 【小家Spring】聊聊Spring中的数据绑定 --- BeanWrapper以及内省Introspector和PropertyDescriptor
#### 每篇一句 > 千古以来要饭的没有要早饭的,知道为什么吗? #### 相关阅读 [[小家Spring]聊聊Spring中的数据转换:Converter.ConversionService ...
- 深入理解Java:内省(Introspector)
深入理解Java:内省(Introspector) 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传 ...
- Java:内省(Introspector)
内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的字段,且 ...
随机推荐
- c# DataTable join 两表连接
转:https://www.cnblogs.com/xuxiaona/p/4000344.html JlrInfodt和dtsource是两个datatable,通过[姓名]和[lqry]进行关联 v ...
- redis Set相关命令
- texinfo - 软件文档系统
DESCRIPTION 描述 Texinfo 是一种文档系统,使用单一的源文件来产生在线文档以及可打印的输出.它主要用于书写软件使用手册. 要查看 Texinfo 语言和相关工具的全面描述,请查看 T ...
- dubbo-源码阅读之Filter默认实现
SPI配置的默认实现 cache=com.alibaba.dubbo.cache.filter.CacheFilter validation=com.alibaba.dubbo.validation. ...
- cd 切换
切换
- input | button | textarea 元素的checked, disabled,hidden属性控制
这三种元素涉及到的checked, disabled,hidden属性的控制方法如下 一.attribute方法: //以下3行,都会影响HTML的( checked | disabled | hid ...
- koa2 的处理请求体koa-bodyparser koa-router 的中间件的学习
1.官网 https://www.npmjs.com/package/koa-router https://www.npmjs.com/package/koa-bodyparser 2. demo / ...
- 在虚拟机中使用maven编译signal server项目记录
前言 安装好 mvn Ubuntu 16.04 JDK 我是从 oracle jdk 11 lts download 网站,复制jdk-11.0.4_linux-x64_bin.tar.gz的链接 w ...
- 什么是平衡树B-Tree?【转】
转载自:https://www.cnblogs.com/dongguacai/p/7239599.html#commentform B-Tree就是我们常说的B树,一定不要读成B减树,否则就很丢人了. ...
- Excel处理
转载:https://www.cnblogs.com/cang12138/p/5606130.html 上面的博友已经讲的很清楚了,但是我们在服务端是获取不到前端上传文件的绝对路径的(因为新浏览器有安 ...