2-java内省机制(Introspector)
来一个简单的示例吧
package com.my.test; import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor; import com.my.bean.User; public class Demo {
/**
* 刘诗华
* 内省机制(Introspector)
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { //获取User类的字节码,不要获取直接父类(Object)的属性
BeanInfo beanInfo = Introspector.getBeanInfo(User.class,Object.class); //获取User类里面的所有属性描述器 返回数组
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) {
//属性字段名
String name = pd.getName();
//属性字段类型
Class type = pd.getPropertyType(); System.out.println(name+"="+type);
} //打印结果如下显示
//id=int
//password=class java.lang.String
//userName=class java.lang.String
}
}
获取Getter和Setter方法
package com.my.test; import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method; import com.my.bean.User; public class Demo {
/**
* 刘诗华
* 内省机制(Introspector)
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { //获取User类的字节码,不要获取直接父类(Object)的属性
BeanInfo beanInfo = Introspector.getBeanInfo(User.class,Object.class); //获取User类里面的所有属性描述器 返回数组
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) {
//获取getter方法
Method readMethod = pd.getReadMethod();
//获取setter方法
Method writeMethod = pd.getWriteMethod(); System.out.println(readMethod);
System.out.println(writeMethod);
} //打印结果如下显示
// public int com.my.bean.User.getId()
// public void com.my.bean.User.setId(int)
// public java.lang.String com.my.bean.User.getPassword()
// public void com.my.bean.User.setPassword(java.lang.String)
// public java.lang.String com.my.bean.User.getUserName()
// public void com.my.bean.User.setUserName(java.lang.String)
}
}
通过内省略机制封两个方法
package com.my.javabean; import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Map; import com.my.bean.User; public class BeanUtil { /***
* Bean对象转换成Map集合
* @param bean
* @return
* @throws Exception
*/
public static Map<String, Object> bean2map(Object bean) throws Exception
{
//创建一个map集合对象
Map<String, Object> map=new HashMap<String, Object>(); BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(),Object.class);
//获取Bean对象的属性描述器
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
//迭代循环属性描述器
for (PropertyDescriptor pd : pds) {
//获取属性名
String propertyName=pd.getName();
//获取属性值,调用 invoke方法
Object propertyValue = pd.getReadMethod().invoke(bean);
//将内容存放到map集合当中
map.put(propertyName, propertyValue);
}
return map;
} /***
* 将Map集合数据封装到Bean对象当中
* T代表数据类型
* @param beanMap 参数Map
* @param beanType Bean对象字节码
* @return
* @throws Exception
*/
public static <T>T map2bean(Map<String, Object> beanMap,Class<T> beanType) throws Exception
{
//创建Bean对象,用T类型来接收 T是在Class<T> beanType这个参数就会确认实际类型
T obj = beanType.newInstance(); BeanInfo beanInfo = Introspector.getBeanInfo(beanType,Object.class);
//获取Bean对象的属性描述器
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) {
//获取属性名
String propertyName=pd.getName();
//从Map集合中取出数据,封装到Bean对象当中
pd.getWriteMethod().invoke(obj, beanMap.get(propertyName));
}
return obj;
} public static void main(String[] args) throws Exception { User u=new User(100,"刘诗华","xxx"); Map<String, Object> m = BeanUtil.bean2map(u);
System.out.println(m);
//{id=100, userName=刘诗华, password=xxx} User user = map2bean(m,User.class);
System.out.println(user);
//User(id=100, userName=刘诗华, password=xxx)
}
}
2-java内省机制(Introspector)的更多相关文章
- java内省机制Introspector
访问JavaBean属性的两种方式 1)直接调用bean的setXXX或getXXX方法: 2)通过内省技术访问(java.beans包提供了内省的API),内省技术访问也提供了两种方式: a)通过P ...
- Java内省机制
转自: https://blog.csdn.net/hahalzb/article/details/5972421 1.java内省机制其实通俗的理解为,对自身的进行一个扫描,这个扫描的对象就是我们普 ...
- java内省机制及PropertyUtils使用方法
背景 一般情况下,在Java中你可以通过get方法轻松获取beans中的属性值.但是,当你事先不知道beans的类型或者将要访问或修改的属性名时,该怎么办?Java语言中提供了一些像java.bean ...
- Java 内省机制
一.内省 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法.JavaBean是一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的 ...
- 就改了get,却不让我set?——Java内省机制的神奇行为举止一例
[相关类库]org.apache.commons.beanutils.BeanUtils,提供对Java反射和自省API的包装,其中底层使用到了Java的内省方法.[内省的一般应用形式]通过类Intr ...
- java 内省(Introspector)
开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都使用反射技术完成此类操作过于麻烦,所以sun公司开发了一套API,专门用于操作java对象的属性. 当然你也可以用反射来操作JavaB ...
- Java的内省机制
我现在的理解就是,Java的内省机制就是针对JavaBean的,可以获取到类的属性名称,以及属性的Getter和Setter方法,应该是在写框架的时候才会用到内省机制,还有一个地方可以用到内省机制,就 ...
- 01-Introspector内省机制
在java领域编程中,内省机制相当的不错,可以省去我们程序员很多的不必要的代码 比如说:在jdbc工具类 我们可以将ResultSet结果集待到 javabean对象中 将http请求报文的数据 转换 ...
- 【小家Spring】Spring IoC是如何使用BeanWrapper和Java内省结合起来给Bean属性赋值的
#### 每篇一句 > 具备了技术深度,遇到问题可以快速定位并从根本上解决.有了技术深度之后,学习其它技术可以更快,再深入其它技术也就不会害怕 #### 相关阅读 [[小家Spring]聊聊Sp ...
随机推荐
- C# to IL 6 Reference and Value Types(引用类型和值类型)
An interface is a reference type, in spite of the fact that it has no code at all. Thus, wecannot in ...
- socket服务器编程的一般思路
socket bind 创建一个listern线程 为每一个连接的client创建一个线程
- redmine和jenkins的ldap登录设置
工具: softeera LDAP browser 流程: Authentication modes » test Name * Host * Port * LDAPS Account Passwo ...
- 使用 Travis 进行持续集成
廖雪峰教程:https://www.liaoxuefeng.com/article/0014631488240837e3633d3d180476cb684ba7c10fda6f6000
- 函数防抖(Debounce)、函数节流 (Throttle)
一篇介绍文章:https://zhuanlan.zhihu.com/p/38313717 演示示例:http://demo.nimius.net/debounce_throttle/ 函数防抖(Deb ...
- php保留两位小数的3种方法
<?php $num = 8.16789; //第一种:利用round()对浮点数进行四舍五入 echo round($num,2).PHP_EOL; //8.17 //第二种:利用sprint ...
- Spring 4 中重定向RedirectAttributes的使用
RedirectAttributes 的使用 @RequestMapping(value = "/redirecttest", produces = "applicati ...
- JDK1.8中如何用ScriptEngine动态执行JS
JDK1.8中如何用ScriptEngine动态执行JS jdk1.6开始就提供了动态脚本语言诸如JavaScript动态的支持.这无疑是一个很好的功能,毕竟Java的语法不是适合成为动态语言.而JD ...
- 自然语言处理之Levenshtien Distance算法研究
自然语言处理中,一个很重要的应用就是问答系统,这里面,涉及到问题和知识库里面的问题的匹配度,从而检索出问题的答案,这个是一个比较常见的应用算法. 编辑距离(Edit Distance),又称Leven ...
- minicom的安装及配置
1. sudo apt-get install minicom 2. 配置 (1). sudo minicom –s (2). (3). 按“A”配置设备,再按回车保存.按”F”,把留空改为NO.回车 ...