Introspector(内省)简单示例 与 简单应用
2016-07-26 00:00
465 查看
简单示例:
运行结果:
--------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包。
这样所有需要图片的地方就只需要调用这一个方法了。
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; }
这样所有需要图片的地方就只需要调用这一个方法了。
相关文章推荐
- 易学设计模式看书笔记(1) - 设计模式的分类
- 给你一组字符串 如 {5,2,3,2,4,5,1,2,1,5},让你输出里面出现次数最多且数值最大的
- Java内存区域与内存溢出异常与Eclipse运行速度调优
- 设计模式(8) - 迭代器模式(iterator)- 实现ArrayList和linkedList的迭代器
- 排序-Perl
- Idea使用技巧
- Adapter初步学习
- Javascript 设计模式学习之一 Constructor(构造器)模式
- 剑指offer(Java)-复杂链表的复制
- 剑指Offer面试题-二叉搜索树与双向链表
- JS正则表达式大全(整理详细且实用)
- solr中文分词
- solr伪集群搭建
- mybatis的配置文件之 mybatis-config.xml
- mysql之driver、url
- oracle之driver、url
- *-mapper.xml配置文件
- ehcache.xml配置
- 安装nginx
- mui ajax请求 登录