zoukankan      html  css  js  c++  java
  • day27(反射之内省机制实现BeanUtils)

        使用内省方式来实现beanUtils往对象里面存值

    public class BeanInfoUtil2 {
    	public static void setPropertyByIntrospector(Object userInfo,  
                Map<String,Object> map) throws Exception {  
    			//map  key=name  value=value
            BeanInfo beanInfo = Introspector.getBeanInfo(userInfo.getClass());  
            PropertyDescriptor[] proDescrtptors = beanInfo.getPropertyDescriptors();  
            if (proDescrtptors != null && proDescrtptors.length > 0) {  
                for (PropertyDescriptor propDesc : proDescrtptors) {  
                	Method method = propDesc.getWriteMethod();
                	if (null==method) {
    					continue;
    				}
                    for (String keys : map.keySet()) {
    					if (method.getName().equals("set"+keys)) {
    						method.invoke(userInfo, map.get(keys));
    					}
    				}
                }  
            }  
        }  
    }
    

        测试类

                    Student s=new Student();
    		Map<String,Object> map=new HashMap<String,Object>();
    		map.put("Name", "张三");
    		map.put("Age", 15);
    		BeanInfoUtil2.setPropertyByIntrospector(s, map);
    		System.out.println(s.getName());
    		System.out.println(s.getAge());        
    

      

          

        

  • 相关阅读:
    代理支持
    CGI
    SSI(服务器端嵌入)
    SSL/TLS 配置
    JSPs
    类加载机制
    JDBC 数据源
    安全管理
    Realm 配置
    Js将序列化成Json格式后日期(毫秒数)转成日期格式
  • 原文地址:https://www.cnblogs.com/fjkgrbk/p/introspection_.html
Copyright © 2011-2022 走看看