简单演示样例:

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. JAVA NIO之Character Set

    明白以下几个概念: 字母集(Character Set),汉字,特殊符号,字母这些都是字符集: 字符编码集(Coded character set),将字符集的字符使用数字进行编码:比如ASCII,就 ...

  2. git 常用命令行整理

    1.创建分支dev git branch dev 2.切换到dev分支 git checkout dev 3.创建并切换本地分支,分支名为dev git checkout -b dev 4.查看本地所 ...

  3. Wild Words

    poj1816:http://poj.org/problem?id=1816 题意:给你n个模板串,然后每个串除了字母,还有?或者*,?可以代替任何非空单个字符,*可以替代任何长度任何串,包括空字符串 ...

  4. ScrollView中嵌套两个ListView

    做的项目中要使用两个ListView在同一个页面上下显示,因为数据源不同,不能通过在Adapter中设置标志位去区分显示,最后只能硬着头皮做一个ScrollView嵌套两个ListView,但按正常情 ...

  5. [OJ] Find Minimum in Rotated Sorted Array

    LintCode 159. Find Minimum in Rotated Sorted Array (Medium) LeetCode 153. Find Minimum in Rotated So ...

  6. 深入浅出 - Android系统移植与平台开发(十一)- Android系统的定制(瘋耔修改篇一)

    首先非常感谢原文作者为我们提供的知识库,因为有你们的贡献,我们的开发难度更显简单 原文 :   http://blog.csdn.net/mr_raptor/article/details/30113 ...

  7. 处理 insert 字段内容包含 单引号 的问题

    问题:postgreSQL insert 字段包含单引号,如:insert into table values('1001','tom'cat'),执行报错: 解决:将单引号替换为两个单引号,如 de ...

  8. 学以致用:让visualstudio爱上sublime

    前言: 经常在vs中工作,但是一些编辑工作却非常喜欢sublime的方式,如果你也是,那我们来当媒婆吧,哈哈. 准备:       Visualstudio一枚        Sublime一枚   ...

  9. 使用Eclipse创建模板并格式化代码

    eclipse设置方式: window-preferences-->java-code Style-formatter 在编写完代码后使用代码格式化程序对代码进行格式化,有利于版本冲突时进行对比 ...

  10. Spark SQL JSON数据处理

    背景   这一篇可以说是“Hive JSON数据处理的一点探索”的兄弟篇.   平台为了加速即席查询的分析效率,在我们的Hadoop集群上安装部署了Spark Server,并且与我们的Hive数据仓 ...