内省,英文中称作introspector。主要对javaBean进行操作,JavaBean是一个特殊的Java类,该类中方法名符合特定的规则(其实就是getXXX,setXXX),我们一般是利用get,set方法来推断属性的名称,而不是直接根据属性来获得名称,因为属性都是私有的,而get,set方法都是共有的。推断规则:如果第二个字母为小写,则首字母小写,例如:

  1. getAge—>age
  2. setage—>age

由于自己根据方法名来推断属性名称非常麻烦,因此我们可以通过内省的方式来调用set,get方法,看看下面的例子:

    @Test
public void test4() {
Person p1 = new Person();
Object value = "zhangsan";
String propertyName = "username"; try {
// 给属性设置值
setProperty(p1, value, propertyName);
// 获得属性值
System.out.println(getProperty(p1, propertyName));
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | IntrospectionException e) {
e.printStackTrace();
}
} private Object getProperty(Object p1, String propertyName)
throws IntrospectionException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
PropertyDescriptor pd1 = new PropertyDescriptor(propertyName,
p1.getClass());
Method methodGetProperty = pd1.getReadMethod();
return methodGetProperty.invoke(p1);
} private void setProperty(Object p1, Object value, String propertyName)
throws IntrospectionException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
PropertyDescriptor pd1 = new PropertyDescriptor(propertyName,
p1.getClass());
Method methodSetProperty = pd1.getWriteMethod();
methodSetProperty.invoke(p1, value);
}

Person.java

public class Person {

    private String username;
private String password;
private int money;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
} }

使用JavaBean中提供的BeanInfo类来操作,这样略微麻烦,但也是一种实现方式:

    private Object getProperty(Object p1, String propertyName)
throws IntrospectionException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
/*PropertyDescriptor pd1 = new PropertyDescriptor(propertyName,
p1.getClass());
Method methodGetProperty = pd1.getReadMethod();
return methodGetProperty.invoke(p1);*/
BeanInfo beanInfo = Introspector.getBeanInfo(p1.getClass());
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor pd : pds){
if(pd.getName().equals(propertyName)){
Method methodGetProperty = pd.getReadMethod();
return methodGetProperty.invoke(p1);
}
}
return null;
}

使用内省方式操作JavaBean的更多相关文章

  1. 使用内省的方式操作JavaBean

    import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; im ...

  2. [新手学Java]使用内省(Introspector)操作JavaBean属性

    获取类bean中的所有属性: @Test //获取类bean中的所有属性 public void test1() throws Exception{ BeanInfo info = Introspec ...

  3. 内省机制(操作javaBean的信息)

    内省机制(操作javaBean的信息) ----是不是联想到了反射机制了哈,这两者有什么区别呢? 1.内省机制和反射机制的联系 ■ 其实内省机制也是通过反射来实现的,而反射是对一切类都适合去动态获取类 ...

  4. 内省操作javabean的属性

    import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector ...

  5. java高新技术-操作javaBean

    1. 对javaBean的简单内省操作 public class IntroSpectorTest { public static void main(String[] args) throws Ex ...

  6. 内省Introspector(反射操作javaBean)

    一:内省是一种特殊的反射,来更方便的操作javaBean对象,通过内省可以获取到类字节码的描述器, 然后解剖每一个字段,获取每个字段的读写方法,即get/set方法的反射,然后获取或者是封装bean的 ...

  7. Linux下用文件IO的方式操作GPIO(/sys/class/gpio)

    通过sysfs方式控制GPIO,先访问/sys/class/gpio目录,向export文件写入GPIO编号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括direction ...

  8. Linux下用文件IO的方式操作GPIO(/sys/class/gpio)(转)

    通过sysfs方式控制GPIO,先访问/sys/class/gpio目录,向export文件写入GPIO编号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括direction ...

  9. Android-Sqlite-OOP方式操作增删改查

    之前写的数据库增删改查,是使用SQL语句来实现的,Google 就为Android开发人员考虑,就算不会SQL语句也能实现增删改查,所以就有了OOP面向对象的增删改查方式 其实这种OOP面向对象的增删 ...

随机推荐

  1. 矩阵分解(rank decomposition)文章代码汇总

    矩阵分解(rank decomposition)文章代码汇总 矩阵分解(rank decomposition) 本文收集了现有矩阵分解的几乎所有算法和应用,原文链接:https://sites.goo ...

  2. Codeforces Round #197 (Div. 2) : B

    也是水题一个,不过稍微要细心点.... 贴代码: #include<iostream> using namespace std; long long n,m; ; int main() { ...

  3. [LeetCode#157] Read N Characters Given Read4

    Problem: The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is ...

  4. WordPress Videowall插件‘page_id’参数跨站脚本漏洞

    漏洞名称: WordPress Videowall插件‘page_id’参数跨站脚本漏洞 CNNVD编号: CNNVD-201310-502 发布时间: 2013-10-23 更新时间: 2013-1 ...

  5. SharePoint 2010顶部链接导航栏的详细操作

    转:http://www.360sps.com/Item/UseTopLink.aspx 在SharePoint 2010环境的页面中,导航链接总体上可以分为两类,一类是显示在左侧的快速启动栏,另一类 ...

  6. 使用Eclipse构建GeoTools项目

    转自:http://hi.baidu.com/liushuigs/item/a62969e6667f9815585dd8b1 由于GeoTools是原本是使用Maven构建的,所以,不能直接将工程导入 ...

  7. Delta-wave

    Delta-wave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. linux 下的sublime

    Sublime Text 2 的安装 : 在官方网站下载Linux版本  Or  执行 #  wget http://c758482.r82.cf2.rackcdn.com/Sublime%20Tex ...

  9. 使用VisualStudio进行单元测试之一

    使用VisualStudio中的单元测试功能,可以很方便的创建单元测试项目.编写单元测试代码以及执行单元测试.而如何在VisualStudio中使用单元测试功能,就是本文和后面几篇想要说的了.    ...

  10. ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA

    Centos5.5 安装Oracle11g客户端,配置了本地的net服务后,用sqlplus连接报错: tnsnames.ora配置如下 orcl = (DESCRIPTION = (ADDRESS ...