简单演示样例:

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. 转载:Source Insight查看ARM汇编源程序 && 高亮显示程序 && Source Insight打开project窗口出错

    (1)Source Insight查看ARM汇编源程序.做ARM嵌入式开发时,有时得整汇编代码,但在SIS里建立PROJECT并ADD TREE的时候,根据默认设置并不会把该TREE里面所有汇编文件都 ...

  2. HDU 1394 Minimum Inversion Number 线段树

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=1394 没看到多组输入,WA了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...

  3. json序列化后日期如何变回来

    日期格式为 日期小于10的时候 占一位 比如 2019年9月1日 2015/9/1 function ChangeDateFormat(cellval) {           var date = ...

  4. 修改tabbar 字体颜色

    NSDictionary *seletedTextAttrs = @{NSForegroundColorAttributeName:[UIColor orangeColor]}; 修改tabbar 字 ...

  5. leetcode第四题:Median of Two Sorted Arrays (java)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  6. aspx、ashx以及cs的关系,viewState

    aspx和ashx关系:aspx就是一种特殊的ashx,aspx对应的类是page,它是实现了IHttpHandler接口,所以说aspx是高级的HttpHandler.aspx中帮我们封装了很多操作 ...

  7. VisualStudio替换所有空行

    [一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/3371620.html] Ctrl+Shift+H 查找内容\r\n\r\n   //如果要替换 ...

  8. C/C++ ceil和floor函数

    ceil 是“天花板" floor 是 “地板”  一个靠上取值,另一个靠下取值,如同天花板,地板. double ceil ( double x ); float ceil ( float ...

  9. $response->decoded_content; 和$response->content; 乱码问题

    centos6.5:/root/podinns/lib#cat t1.pl use LWP::UserAgent; use HTTP::Date qw(time2iso str2time time2i ...

  10. 嵌套滚动demo

    https://github.com/luv135/NestedScrollingDemo https://github.com/ggajews/nestedscrollingchildviewdem ...