zoukankan      html  css  js  c++  java
  • 使用内省方式操作JavaBean

    内省,英文中称作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;
        }
  • 相关阅读:
    Spring MVC 完全注解方式配置web项目
    spring WebServiceTemplate 调用 axis1.4 发布的webservice
    修改Intellij Idea 创建maven项目默认Java编译版本
    Git Commit提交规范和IDEA插件Git Commit Template的使用
    myEclipse10安装以及破解
    ES6中Map与其他数据结构的互相转换
    ES6用来判断数值的相关函数
    WebStorm使用码云插件问题
    Css解决表格超出部分用省略号显示
    Js/Jquery获取网页屏幕可见区域高度
  • 原文地址:https://www.cnblogs.com/qitian1/p/6461871.html
Copyright © 2011-2022 走看看