javabeans 内省 introspector BeanUtils
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"));
}
}
javabeans 内省 introspector BeanUtils的更多相关文章
- JavaBeans与内省(Introspector)
		
JavaBean与Introspector 反射和内省操作很多时候都是在以后要做框架的时候作用非常大. 现在你学的是面向对象编程,即:你所写代码都能够找到对应的类或接口,找到具体的方法写出对应的 ...
 - Java 内省(Introspector)和 BeanUtils
		
人生若只如初见,何事秋风悲画扇. 概述 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传递数据信息, ...
 - 内省(introspector)------>JavaBean
		
内省(introspector)------>JavaBean 1.问什么要学内省? 开发框架时,经常需要Java对象的属性来来封装程序的数据,每次使用反射技术完成此操作过于 ...
 - 内省(Introspector)
		
内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法 目的:主要用于传递数据信息,这种类中的方法主要用于访问私有的字段(且方法名符合某种命名规则) p ...
 - JavaEE JavaBean 反射、内省、BeanUtils
		
JavaEE JavaBean 反射.内省.BeanUtils @author ixenos JavaBean是什么 一种规范,表达实体和信息的规范,便于封装重用. 1.所有属性为private2.提 ...
 - 深入理解Java:内省(Introspector)
		
深入理解Java:内省(Introspector) 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传 ...
 - JAVA中反射机制五(JavaBean的内省与BeanUtils库)
		
内省(Introspector) 是Java 语言对JavaBean类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的字段,且方法 ...
 - Java 内省(Introspector)深入理解
		
Java 内省(Introspector)深入理解 一些概念: 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类 ...
 - 【小家Spring】聊聊Spring中的数据绑定 --- BeanWrapper以及内省Introspector和PropertyDescriptor
		
#### 每篇一句 > 千古以来要饭的没有要早饭的,知道为什么吗? #### 相关阅读 [[小家Spring]聊聊Spring中的数据转换:Converter.ConversionService ...
 
随机推荐
- [Silverlight][linq to sql]不能找到linq to sql自动生成类型
			
最近在做Silverlight项目,结合使用了WCF RIA service,通过linq to sql自动生成model类型,使用起来非常方便.具体可见linq to sql之silverlight ...
 - Linux小知识(1): bash中执行数据库的相关操作
			
工作中经常会遇到命令行操作数据库或登陆至其他服务器等情况,往往需要通过命令行进入相关的数据库或登陆至相关的服务器,再执行指令操作,因此有没有方法,在命令行或bash脚本中模拟进入数据库或登陆服务器操作 ...
 - plsql窗口列表保持
			
使用plsql窗口列表保持方法如下: 1.工具——首选项——用户界面——自动保存桌面 选一下此项就保存你当前的窗口布局了,下次启动就不用再设置了. 英语版自己对照.
 - centos系统安装mysql
			
方式一. 通过yum install mysql-server安装mysql服务器.chkconfig mysqld on设置开机启动,并service mysqld start启动mysql服务,并 ...
 - window下隐藏apache版本和PHP脚本等敏感信息
			
隐藏Apache信息 1.1 主配置中启用httpd-default.conf 文件: conf/httpd.Conf 找到httpd-default.conf,删除前面的注释“#”,改成如下 Inc ...
 - python标准库 - socket
			
地址簇(address family) socket.AF_UNIX # (UNIX Domain Sockets) socket.AF_INET # ipv4 socket.AF_INET6 # i ...
 - MyEclipse导入JAVA工程显示红色叉叉的解决方法
			
当我们有时候导入一个新的工程的时候可能会出现以下这种情况,基本上是因为jar包路径的问题. 解决方法如下: 1.右击工程,选择properties 2.选择 Java Build Path -> ...
 - Excel汇总多个页卡数据到一个页卡
			
首先新建一个页卡放到最前面,页卡处右键,选择查看代码,选择需要汇总的页卡,输入以下代码,运行即可: 1.如果需要把全部数据都汇总到一个页卡 Sub 合并当前工作簿下的所有工作表() Applicati ...
 - centos 7(1611)安装笔记
			
麻烦 前天我把双系统笔记本里的 deepin 的磁盘分区直接从 Windows 7 磁盘管理里格式化了,结果悲催了,开不了机了,显示: 我以为是 Windows 7 的引导没了,就进 PE 修复了 ...
 - ubuntu 显示隐藏文件
			
原文链接 http://blog.csdn.net/happyjiahan/article/details/6023496 方法1.使用命令ls -a显示所有的文件,包括隐藏文件 方法2.在桌面化操作 ...