zoukankan      html  css  js  c++  java
  • JAVA内省机制

     

    内省是反射的一种特例

    由于在框架底层中要频繁地操作javabean,而利用反射操作比较麻烦。所以为了方便操作javabean,sun公司开发出一套API提高效率

     

    Java内省机制是针对JavaBean进行操作的

    public class Person {
        
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        private int age;
     
    }
    public class TestIntrospector {
     
        @Test
        public void tes1() throws Exception {
            Class<?> cl = Class.forName("com.irish.aop.cc.Person");
            //在bean上进行内省
            BeanInfo beaninfo = Introspector.getBeanInfo(cl, Object.class);
            PropertyDescriptor[] pro = beaninfo.getPropertyDescriptors();
            Person p = new Person();
            System.out.print("Person的属性有:");
            for (PropertyDescriptor pr : pro) {
                System.out.print(pr.getName() + " ");
            }
            System.out.println("");
            for (PropertyDescriptor pr : pro) {
                Method writeme = pr.getWriteMethod();
                if (pr.getName().equals("name")) {
                    writeme.invoke(p, "xiong");
                }
                if (pr.getName().equals("age")) {
                    writeme.invoke(p, 23);
                }
                Method method = pr.getReadMethod();
                System.out.print(method.invoke(p) + " ");
     
            }
        }
     
        @Test
        public void test2() throws Exception {
            PropertyDescriptor pro = new PropertyDescriptor("name", Person.class);
            Person preson=new Person();
            Method  method=pro.getWriteMethod();
            method.invoke(preson, "xiong");
            System.out.println(pro.getReadMethod().invoke(preson));
        }
    }

    怎么让后台的Model对象统一的接收表单提交过来的参数

    Servlet

        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            user u=new user();
            populate(request.getParameterMap(),u);
            System.out.println(u);
            
        }
        
        private void populate(Map<String,String[]> map,user u){
            try {
                Map<String,String[]> params=map;
                //1 获得 java Bean的描述信息
                BeanInfo info =Introspector.getBeanInfo(user.class);
                //2 获得 User中的属性信息
                PropertyDescriptor [] pds =info.getPropertyDescriptors();
                //3 遍历属性信息
                for (PropertyDescriptor pd : pds) {
                    String[] param=params.get(pd.getName());
                    if (param!=null && param.length>0) {
                        pd.getWriteMethod().invoke(u, param[0]);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    JavaBean

    public class user {
        private String name;
        private String password;
        public user(){
            
            super();
        }
        
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public String getPassword() {
            return password;
        }
     
        public void setPassword(String password) {
            this.password = password;
        }
        
        @Override
        public String toString() {
            return "user [name=" + name + ", password=" + password + " ]";
        }
    }

    Jsp

        <body>
            <form action="/JavaBean/BServlet" method="post" >
                用户名:<input type="text" name="name" /><br>
                密码:<input type="password" name="password" /><br>
            
                <input type="submit" /><br>
            </form>
        </body>
  • 相关阅读:
    Ubuntu14.04升级cmake版本的方法
    在ubuntu16.04-32bits 下编译vlc和vlc-qt开源项目
    从Ubuntu 14.04 LTS版升级到Ubuntu 16.04 LTS
    如何使用Heartbeat,组建一个高可用性的mysql集群
    VLC和Qt结合编写流媒体rtsp播放器
    How to Analyze "Deadlocked Schedulers" Dumps?---WINDBG
    sqlserver-kit.org
    SQLSERVER ----improvedk
    sql server博客
    分享]国外最新安全推文整理
  • 原文地址:https://www.cnblogs.com/moris5013/p/10985765.html
Copyright © 2011-2022 走看看