简单演示样例:

package com.asdfLeftHand.test;

import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; class Person { private String name;
private int age; public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} } public class IntrospectorTest { /**
* @param args
* @throws IntrospectionException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//
Person person = new Person();
person.setAge(22);
person.setName("小强"); BeanInfo beanInfo = Introspector.getBeanInfo(person.getClass());
//BeanInfo beanInfo = Introspector.getBeanInfo(Person.class); System.out.println("--------BeanDescriptor--------");
BeanDescriptor beanDesc = beanInfo.getBeanDescriptor();
Class cls = beanDesc.getBeanClass();
System.out.println(cls.getName()); System.out.println("--------MethodDescriptor-------");
MethodDescriptor[] methodDescs = beanInfo.getMethodDescriptors();
for (int i = 0; i < methodDescs.length; i++) {
Method method = methodDescs[i].getMethod();
System.out.println(method.getName());
} System.out.println("--------PropertyDescriptor------"); PropertyDescriptor[] propDescs = beanInfo.getPropertyDescriptors();
for(int i = 0; i < propDescs.length; i++) {
Method methodR = propDescs[i].getReadMethod();
if (methodR != null) {
System.out.println("读方法:"+methodR.getName());
Object o= methodR.invoke(person);
System.out.println(methodR.getName()+":"+o);
}
Method methodW = propDescs[i].getWriteMethod();
if (methodW != null) {
System.out.println("写方法:"+methodW.getName());
if(methodW.getName().equals("setName")){
methodW.invoke(person,"小王");
System.out.println("调用"+methodW.getName()+"方法后的值为:"+person.getName());//此处为了方便就直接用person.getName()方法了
}
}
}
} }

执行结果:

--------BeanDescriptor--------

com.asdfLeftHand.test.Person

--------MethodDescriptor-------

hashCode

wait

setAge

notifyAll

equals

wait

wait

toString

setName

getAge

notify

getClass

getName

--------PropertyDescriptor------

读方法:getAge

getAge:22

写方法:setAge

读方法:getClass

getClass:class com.asdfLeftHand.test.Person

读方法:getName

getName:小强

写方法:setName

调用setName方法后的值为:小王

一个简单应用:利用内省简化一系列类似的方法为一个通用的方法。

部分代码:

用到common BeanUtils包。

/**
* 有一张图片Image表,存有何种对象相应的图像(如 用户头像),用hql语句查处相应的图片集合,
* 各种对象字段有差别可是查询方法相似,就写一个通用的方法(相对通用)
* 得到某个对象集合的图片。
* key:guid+id,value:address+fileName
* @param list
* @param type
* @return
*/
public Map<String,String> getImagesMap(List<?> list,int imageType) {
Map<String, String> imagesMap = new HashMap<String, String>();
List<?> entityList = list;
for(int i=0;i<entityList.size();i++){
Object entity = entityList.get(i);
String id = "";
String guid = "";
try {
BeanInfo bi = Introspector.getBeanInfo(entity.getClass(), Object.class);
PropertyDescriptor[] props = bi.getPropertyDescriptors();
for (int i2 = 0; i2 < props.length; i2++) {
String str = props[i2].getName();
if(str.equals("guid")){
guid = BeanUtils.getProperty(entity, str);
}else if(str.endsWith("ID")){
id = BeanUtils.getProperty(entity, str);
}
}
} catch (Exception e) {
e.printStackTrace();
}
String hql2 = "from Image where guid='"+guid+"' and FKID='"+id+"' and type="+imageType";
List<Image> list2 = imageDao.query(hql2);
//,,,
}
return imagesMap; }

这样全部须要图片的地方就仅仅须要调用这一个方法了。

Introspector(内省)简单演示样例 与 简单应用的更多相关文章

  1. JBoss 系列九十六:JBoss MSC - 简介及一个简单演示样例

    什么是 JBoss MSC JBoss MSC 即 JBoss Modular Service Container,是第三代 JBoss 产品 JBoss 7和WildFfly的内核,JBoss MS ...

  2. Thrift的安装和简单演示样例

    本文仅仅是简单的解说Thrift开源框架的安装和简单使用演示样例.对于具体的解说,后面在进行阐述. Thrift简述                                           ...

  3. [hadoop系列]Pig的安装和简单演示样例

    inkfish原创,请勿商业性质转载,转载请注明来源(http://blog.csdn.net/inkfish ).(来源:http://blog.csdn.net/inkfish) Pig是Yaho ...

  4. 一则简单演示样例看Oracle的“无私”健壮性

    Oracle的强大之处就在于他能总帮助让你选择正确的运行计划,即使你给了它错误的指示. 实验: 1. 创建測试表: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZ ...

  5. Android通过startService播放背景音乐简单演示样例

    关于startService的基本使用概述及其生命周期可參见博客<Android中startService的使用及Service生命周期>. 本文通过播放背景音乐的简单演示样例,演示sta ...

  6. 百度地图 Android SDK - 检索功能使用的简单演示样例

    百度地图 SDK 不仅为广大开发人员提供了炫酷的地图展示效果.丰富的覆盖物图层,更为广大开发人员提供了多种 LBS 检索的能力. 通过这些接口,开发人员能够轻松的訪问百度的 LBS 数据,丰富自己的移 ...

  7. MyBatis对数据库的增删改查操作,简单演示样例

    之前一直有用Hibernate进行开发.近期公司在使用Mybatis.依据网上的演示样例,做了一个简单的Demo,以便日后复习 使用XMl方式映射sql语句 整体结构例如以下图 watermark/2 ...

  8. [Android]RecyclerView的简单演示样例

    去年google的IO上就展示了一个新的ListView.它就是RecyclerView. 下面是官方的说明,我英语能力有限,只是我大概这么理解:RecyclerView会比ListView更具有拓展 ...

  9. 虚幻4Matinee功能 基本概念及简单演示样例(Sequence编辑器)

    虚幻4提供的Matinee功能十分强大,能够用来制作动画.录制视频. 它的核心想法是在Matinee编辑器内提供一套自己的时间坐标系,在这个相对时间内通过调节actor的属性来改变actor的状态,进 ...

随机推荐

  1. WARNING L15: MULTIPLE CALL TO SEGMENT

    原网页:http://www.cnblogs.com/CuriosityWzk/archive/2011/12/25/2301090.html WARNING L15: MULTIPLE CALL T ...

  2. 工作流(worfflow)

    -- 工作流(Workflow)就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现某个预期的业务 ...

  3. 【转】ant命令总结

    http://feiyeguohai.iteye.com/blog/1295922 ant命令总结 1 Ant是什么?  Apache Ant 是一个基于 Java的生成工具. 生成工具在软件开发中用 ...

  4. Razor模板引擎

    Razor模板引擎 阅读目录 一.简介 二.非Mvc中使用Razor 三.总结 回到目录 一.简介 在MVC以外的场景中,我们往往需要完成一些模板引擎生成代码或页面的工作:在以前我们一般常用的有Raz ...

  5. Aircrack-ng官方文档翻译[中英对照]---Airdecap-ng

    Aircrack-ng官方文档翻译---Airdecap-ng   Description[简介] With airdecap-ng you can decrypt WEP/WPA/WPA2 capt ...

  6. activemq启动不起来,报错Address already in use: JVM_Bind

    之前莫名其妙的activemq怎么都启动不起来后来多方查询是因为widows 的ICS服务. 解决方案是,我的电脑上邮件,选择服务,然后在服务中找到Internet Connection Sharin ...

  7. EasyUI 树形菜单tree 定义图标

    { "id":1, "text":"Folder1", "iconCls":"icon-save", ...

  8. 李洪强iOS开发Swift篇—09_属性

    李洪强iOS开发Swift篇—09_属性 一.类的定义 Swift与Objective-C定义类的区别 Objective-C:一般需要2个文件,1个.h声明文件和1个.m实现文件 Swift:只需要 ...

  9. hadoop2.2编程:序列化

    测试序列化后的长度 提示:需要用到的类,以及继承关系如下: 1.java.lang.Object |__ java.io.OutputStream |__ java.io.ByteArrayOutpu ...

  10. bzoj1049

    第一问类似最长上升序列,只不过因为要满足能修改所以不能直接求比如2 3 4 4 5 最长上升序列长是4,但是最少修改是2,因为一个这个最长上升序列不能保持不变因此我们对a[i]-i,然后求这个新序列a ...