使用内省方式操作JavaBean

举报
江南一点雨 发表于 2021/08/17 00:33:05 2021/08/17
【摘要】 内省,英文中称作introspector。主要对javaBean进行操作,JavaBean是一个特殊的Java类,该类中方法名符合特定的规则(其实就是getXXX,setXXX),我们一般是利用get,set方法来推断属性的名称,而不是直接根据属性来获得名称,因为属性都是私有的,而get,set方法都是共有的。推断规则:如果第二个字母为小写,则首字母小写,例如: ge...

内省,英文中称作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); }
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

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; }

}
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

使用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; }
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

文章来源: wangsong.blog.csdn.net,作者:_江南一点雨,版权归原作者所有,如需转载,请联系作者。

原文链接:wangsong.blog.csdn.net/article/details/45368113

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。