博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c/s角度看待JavaBean,及关于内省
阅读量:2063 次
发布时间:2019-04-29

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

package com.weiday12_1.www;import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Map;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.collections.map.HashedMap;import bean.Student;/** * 内省依赖反射,归根到底还是反射,只不过是对反射的一个包装,是用起来更加顺手。 * 值得说一下的是使用BeanUtils是必须导入Collections.jar * @author 74087 * */public class BeanInfoTest {	public static void main(String[] args) throws Exception {		Class clazz = Class.forName("bean.Student");		Object student = clazz.newInstance();		//useBookInfo();		//useBeanUtilsP(student);		//useUtilsM(student);	}	public static void useUtilsM(Object student) throws IllegalAccessException,			InvocationTargetException {		//我们还可以使用map来进行赋值		Map
m = new HashedMap(); m.put("name","魏金浩"); m.put("num",23); BeanUtils.populate(student, m); System.out.println(student); } public static void useBeanUtilsP(Object student) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { BeanUtils.setProperty(student, "name", "魏金浩"); BeanUtils.setProperty(student, "num", "13"); String name = BeanUtils.getProperty(student,"name"); String num = BeanUtils.getProperty(student, "num"); System.out.println("name=" + name + "num=" + num); } public static void useBookInfo() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IntrospectionException, InvocationTargetException { Class cs = Class.forName("bean.Student"); Student s = (Student)cs.newInstance(); BeanInfo bi = Introspector.getBeanInfo(cs); PropertyDescriptor[] pd = bi.getPropertyDescriptors(); //我们实际的到的数组中有三个PropertyDescriptor对象,从第二个开始是我们的属性描述 if(pd != null) { Method mt1 =pd[1].getWriteMethod(); System.out.println(pd[1].getName());//这里我们可以来自己实现方法达到灵活设置属性和得到属性,但是我们不用这摸麻烦应为commons组件已经为我们实现了 System.out.println(pd[1].getPropertyType()); Method mt2 = pd[2].getWriteMethod(); System.out.println(pd[2].getName()); System.out.println(pd[2].getPropertyType()); if(mt1 != null){ mt1.invoke(s, "魏金浩"); } if(mt2 != null){ mt2.invoke(s, 23); } } System.out.println("name=" + s.getName() + "num=" + s.getNum()); }}

这里最主要的是BeanInfo这个接口,我们可以实现它来达到早作bean的目的,但是这样工作量过于大了,所以我们使用Introspector类的静态方法得到关于该类的BeanInfo对象。该方法在 Java Bean 上进行内省,了解其所有属性、公开的方法和事件。

转载地址:http://quelf.baihongyu.com/

你可能感兴趣的文章
PHPstudy中遇到的坑No input file specified,以及传到linux环境下遇到的坑,模板文件不存在
查看>>
TP5.1事务操作和TP5事务回滚操作多表
查看>>
composer install或composer update 或 composer require phpoffice/phpexcel 失败解决办法
查看>>
TP5.1项目从windows的Apache服务迁移到linux的Nginx服务需要注意几点。
查看>>
win10安装软件 打开时报错 找不到 msvcp120.dll
查看>>
PHPunit+Xdebug代码覆盖率以及遇到的问题汇总
查看>>
PHPUnit安装及使用
查看>>
PHP项目用xhprof性能分析(安装及应用实例)
查看>>
composer安装YII
查看>>
Sublime text3快捷键演示
查看>>
sublime text3 快捷键修改
查看>>
计算机底层是什么东西?
查看>>
关于PHP几点建议
查看>>
硬盘的接口、协议
查看>>
安装系统之一 U盘启动盘制作
查看>>
安装系统之二 U盘启动盘制作---UEFI版
查看>>
安装系统之四 U盘装GHOST XP教程
查看>>
安装系统之五 U盘装原版XP教程
查看>>
安装系统之六 U盘装GHOST WIN7教程
查看>>
安装系统之八 U盘装GHOST WIN8教程
查看>>