javabeans 内省 introspector BeanUtils
2018-06-18 03:18:42来源:未知 阅读 ()
javaBeans 属性的概念
不只是字段,而是其get set 方法
且该get方法有返回值的称为属性,继承Object类的getClass方法
package com.swift.demo1; public class Person { String name; int age; String password; 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; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAd() {//这个算一个属性,虽让没有字段,但如果没有返回值不算一个属性 return "getAd....."; } }
属性个数
package com.swift.demo1; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import org.junit.jupiter.api.Test; public class TestIntro { @Test public void test1() throws Exception { BeanInfo info=Introspector.getBeanInfo(Person.class); PropertyDescriptor[] pds=info.getPropertyDescriptors(); for(PropertyDescriptor des:pds) { System.out.println(des.getName()); } } }
阻止父类的getClass属性用
package com.swift.demo1; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import org.junit.jupiter.api.Test; public class TestIntro { @Test public void test1() throws Exception { BeanInfo info=Introspector.getBeanInfo(Person.class,Object.class); PropertyDescriptor[] pds=info.getPropertyDescriptors(); for(PropertyDescriptor des:pds) { System.out.println(des.getName()); } } }
BeanUtils使用jar包
需要两个:
都可以在Apache网站下载
BeanUtils具有比Introspector更强大的功能,可以在基本数据类型间直接转换,也可以把文本框中的字符串通过注册器转换器进行转换
自己转日期格式
package com.swift.demo1; import java.lang.reflect.InvocationTargetException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; import org.junit.Test; public class TestUtils { @Test public void test() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Person p=new Person(); ConvertUtils.register(new Converter() { @Override public Object convert(Class type, Object value) { String str=(String) value; SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); try { return sdf.parse(str); } catch (ParseException e) { throw new RuntimeException(e);// } } }, Date.class); BeanUtils.setProperty(p, "name", "swift"); BeanUtils.setProperty(p, "age", "30"); BeanUtils.setProperty(p, "password", "123"); BeanUtils.setProperty(p, "date", "2018-02-19"); System.out.println(p.getName()); System.out.println(BeanUtils.getProperty(p, "name")); System.out.println(BeanUtils.getProperty(p, "age")); System.out.println(BeanUtils.getProperty(p, "password")); System.out.println(BeanUtils.getProperty(p, "date")); } }
可以用现成的
package com.swift.demo1; import java.lang.reflect.InvocationTargetException; import java.util.Date; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.locale.converters.DateLocaleConverter; import org.junit.Test; public class TestUtils { @Test public void test() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Person p=new Person(); ConvertUtils.register(new DateLocaleConverter(),Date.class); BeanUtils.setProperty(p, "name", "swift"); BeanUtils.setProperty(p, "age", "30"); BeanUtils.setProperty(p, "password", "123"); BeanUtils.setProperty(p, "date", "2018-02-19"); System.out.println(p.getName()); System.out.println(BeanUtils.getProperty(p, "name")); System.out.println(BeanUtils.getProperty(p, "age")); System.out.println(BeanUtils.getProperty(p, "password")); System.out.println(BeanUtils.getProperty(p, "date")); } }
集合map加到BeanUtils
package com.swift.demo1; import java.lang.reflect.InvocationTargetException; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.locale.converters.DateLocaleConverter; import org.junit.Test; public class TestUtils { @Test public void test() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Person p=new Person(); ConvertUtils.register(new DateLocaleConverter(),Date.class); BeanUtils.setProperty(p, "name", "swift"); BeanUtils.setProperty(p, "age", "30"); BeanUtils.setProperty(p, "password", "123"); BeanUtils.setProperty(p, "date", "2018-02-19"); System.out.println(p.getName()); System.out.println(BeanUtils.getProperty(p, "name")); System.out.println(BeanUtils.getProperty(p, "age")); System.out.println(BeanUtils.getProperty(p, "password")); System.out.println(BeanUtils.getProperty(p, "date")); } @Test public void test1() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Person p=new Person(); ConvertUtils.register(new DateLocaleConverter(),Date.class); Map<String, String> map=new HashMap<String, String>(); map.put("name", "swift"); map.put("age", "30"); map.put("password", "123"); map.put("date", "2018-02-19"); BeanUtils.populate(p, map); System.out.println(p.getName()); System.out.println(BeanUtils.getProperty(p, "name")); System.out.println(BeanUtils.getProperty(p, "age")); System.out.println(BeanUtils.getProperty(p, "password")); System.out.println(BeanUtils.getProperty(p, "date")); } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:Struts2(一)— 入门
- 【小家Spring】Spring IoC是如何使用BeanWrapper和Java内省 2019-08-16
- 内省(Introspector) 2018-12-17
- Java 基础【18】 反射与内省 2018-12-09
- 使用Dto将数据封装成普通的JavaBeans 2018-07-16
- 用企业级JavaBeans前需要考虑几个因素 2008-02-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash