博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javabeans 内省 introspector BeanUtils
阅读量:6985 次
发布时间:2019-06-27

本文共 6193 字,大约阅读时间需要 20 分钟。

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
map=new HashMap
(); 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")); }}

 

转载于:https://www.cnblogs.com/qingyundian/p/8453684.html

你可能感兴趣的文章
设计模式学习02—工厂模式
查看>>
html5--6-10 CSS选择器7--伪类选择器
查看>>
激光数据匹配(MATLAB Robotics System Toolbox)
查看>>
file_put_contents执行返回false,file_put_contents false(linux服务器httpd)
查看>>
JavaScript学习总结一(String对象的用法)
查看>>
处理手势冲突和错乱的一点经验
查看>>
Struts2防止表单重复提交
查看>>
[转]Python格式化输出
查看>>
CSS - 修改input - placeholder 和 readonly 的样式
查看>>
在多线程情况下 局部变量与全局变量 哪个比较安全呢
查看>>
算法评测
查看>>
POJ 2773 Happy 2006
查看>>
UBIFS介绍 - MTD网站
查看>>
如何使用ITEXTSHARP将HTML代码字符串写进PDF
查看>>
Oracle SQL CPU占用高
查看>>
Maya 2015 中英文切换
查看>>
C语言的字符串分割
查看>>
Arduino可穿戴开发入门教程Windows平台下安装Arduino IDE
查看>>
BpBinder 转换为 BpCameraService 流程
查看>>
李洪强经典面试题150-设计模式
查看>>