欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java反射中java.beans包学习总结

程序员文章站 2022-10-19 20:32:47
之前一直以为propertyeditor是springmvc提供的,今天才知道这个是jdk提供的,汗颜啊! 这个东西能干啥用呢?可以把字符串转成一个bean对象,sp...

之前一直以为propertyeditor是springmvc提供的,今天才知道这个是jdk提供的,汗颜啊!

这个东西能干啥用呢?可以把字符串转成一个bean对象,spring mvc在把form表单映射到controller的入参对象就是利用的这个东西。

看一个例子:

nodedo.java:一个标准的javabean对象

public class nodedo {
	private string name;
	private string email;
	private date date;
	public string getname() {
		return name;
	}
	public void setname(string name) {
		this.name = name;
	}
	public string getemail() {
		return email;
	}
	public void setemail(string email) {
		this.email = email;
	}
	public date getdate() {
		return date;
	}
	public void setdate(date date) {
		this.date = date;
	}
	@override
	public string tostring() {
		return "nodedo [name=" + name + ", email=" + email + ", date=" + dodedoeditor.sdf.format(date) + "]";
	}
}

testdo.java一样,里面引用了nodedo:

public class testdo { 
  private string nodename; 
  private nodedo nodedo; 
  public string getnodename() { 
    return nodename; 
  } 
  public void setnodename(string nodename) { 
    this.nodename = nodename; 
  } 
  public nodedo getnodedo() { 
    return nodedo; 
  } 
  public void setnodedo(nodedo nodedo) { 
    this.nodedo = nodedo; 
  } 
}

我们想实现类似这样的功能:

public static void main(string[] args) throws exception{ 
    map<string, string> parameters = new hashmap<string, string>(){ 
      { 
        put("nodename", "小胖测试"); 
        put("nodedo", "xiaopang|xiaopang@163.com|2015-10-20 12:00:00"); 
      } 
    };  
    testdo testdo = convert(parameters); 
    system.out.println(testdo.getnodename()); 
    system.out.println(testdo.getnodedo()); 
  }

把parameters这个map转化成testdo的对象,该如何来做呢?

(1)首先要定义一个用来转化property的propertyeditor:

public class dodedoeditor extends propertyeditorsupport{ 
   
  public static final simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); 
   
  @override 
  public void setastext(string text) throws illegalargumentexception{ 
    string[] tokens = text.split("\\|"); 
    nodedo nodedo = new nodedo(); 
    nodedo.setname(tokens[0]); 
    nodedo.setemail(tokens[1]); 
    try{ 
      nodedo.setdate(sdf.parse(tokens[2])); 
    }catch(parseexception e){ 
      throw new illegalargumentexception(e); 
    } 
    setvalue(nodedo); 
  } 
} 

(2)做转化:

public class propertyeditorsample { 
 
  static{ 
    propertyeditormanager.registereditor(nodedo.class, dodedoeditor.class); 
  } 
   
  public static void main(string[] args) throws exception{ 
    map<string, string> parameters = new hashmap<string, string>(){ 
      { 
        put("nodename", "小胖测试"); 
        put("nodedo", "xiaopang|xiaopang@163.com|2015-10-20 12:00:00"); 
      } 
    };  
    testdo testdo = convert(parameters); 
    system.out.println(testdo.getnodename()); 
    system.out.println(testdo.getnodedo()); 
  } 
 
  private static testdo convert(map<string, string> parameters)throws exception { 
    testdo testdo = new testdo(); 
    beaninfo bi = introspector.getbeaninfo(testdo.class);  
    propertydescriptor[] pds = bi.getpropertydescriptors();  
    for(propertydescriptor pd : pds){ 
      class<?> propertytype = pd.getpropertytype(); 
      method writemethod = pd.getwritemethod(); 
      if(propertytype == class.class){ 
        //ignore 
      }else if(propertytype == string.class){ 
        writemethod.invoke(testdo, parameters.get(pd.getname())); 
      }else{ 
        propertyeditor editor = propertyeditormanager.findeditor(propertytype); 
        if(editor != null){ 
          editor.setastext(parameters.get(pd.getname())); 
          writemethod.invoke(testdo, editor.getvalue()); 
        }else{ 
          system.out.println("no editor for:"+pd.getname()); 
        } 
      } 
    } 
    return testdo; 
  } 
 
}

其实关键点就两句话

<pre name="code" class="java">editor.setastext(parameters.get(pd.getname()));//1 
editor.getvalue();//2 
//因此,一般都会在setastext中去调用setvalue()来保存转化以后的值,这样通过getvalue()就能获取到了 

以上就是本次整理的全部内容,如果大家还有任何不明白的地方,可以在下方留言讨论,感谢大家对的支持。