简单演示样例:

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. asp.net管道模型

    查了很多资料,终于大概弄懂管道模型(注意并非指定是asp.net范畴)是个什么概念了,其实就是从Unix移植过来的一种概念,也可以说是一种模式吧(只允许一头读,一头写,并且读完了就会自动消失). as ...

  2. Rewrite Path in Asp.Net MVC Project

    // Change the current path so that the Routing handler can correctly interpret // the request, then ...

  3. 【转载】C# Tutorial - Simple Threaded TCP Server

    http://tech.pro/tutorial/704/csharp-tutorial-simple-threaded-tcp-server In this tutorial I'm going t ...

  4. 设置UIButton的文字居右显示 去掉点击默认置灰效果

    1.设置UIButton的文字居右显示 [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight]; ...

  5. Unreal Engine4 蓝图入门

    微信公众号:UE交流学习    UE4开发群:344602753 蓝图是Unreal Engine的特点,用C++编程固然好,但是效率要低很多,主要是国内资料比较少,所以不太容易学习,用蓝图编程可以节 ...

  6. leetcode面试准备:Contains Duplicate I && II

    1 题目 Contains Duplicate I Given an array of integers, find if the array contains any duplicates. You ...

  7. [cocos2d demo]认字小游戏

    2013.9.5更新第二版 游戏分三个场景,分别为主场景,加载场景以及游戏场景,游戏场景分为背景层,逻辑层以及UI层 1.背景:旋转太阳,移动波浪,漂浮的云 2.UI层:随机生成字附带在帆船上移动,当 ...

  8. [LeetCode#161] One Edit Distance

    Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...

  9. active directory 学习和概念整理

    第一,在局域网内,如何管理计算机上的资源,需要一个管理策略. 微软提供了两种:工作组和域.两者区别就是,工作组是自治的,组内的计算机个个都作为独立.对等的自治实体而存在.恩,这也是以太网的设计初衷. ...

  10. Android 应用页面延缓载入

    1.新建一个线程,使用handle的延缓运行线程 new Handler().postDelayed(new Runnable() { // 为了减少代码使用匿名Handler创建一个延时的调用 pu ...