什么是Java内省:内省是Java语言对Bean类属性、事件的一种缺省处理方法。

Java内省的作用:一般在开发框架时,当需要操作一个JavaBean时,如果一直用反射来操作,显得很麻烦;所以sun公司开发一套API专门来用来操作JavaBean

  1. package com.javax.iong.javabean0301;
  2. public class Person {
  3. private String name;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. public int getAge() {
  11. return age;
  12. }
  13. public void setAge(int age) {
  14. this.age = age;
  15. }
  16. private int age;
  17. }
  1. package com.javax.iong.javabean0301;
  2. import java.beans.BeanInfo;
  3. import java.beans.Introspector;
  4. import java.beans.PropertyDescriptor;
  5. import java.lang.reflect.Method;
  6. import org.junit.Test;
  7. public class Test1 {
  8. @Test
  9. public void tes1() throws Exception {
  10. Class<?> cl = Class.forName("com.javax.iong.javabean0301.Person");
  11. // 在bean上进行内省
  12. BeanInfo beaninfo = Introspector.getBeanInfo(cl, Object.class);
  13. PropertyDescriptor[] pro = beaninfo.getPropertyDescriptors();
  14. Person p = new Person();
  15. System.out.print("Person的属性有:");
  16. for (PropertyDescriptor pr : pro) {
  17. System.out.print(pr.getName() + " ");
  18. }
  19. System.out.println("");
  20. for (PropertyDescriptor pr : pro) {
  21. // 获取beal的set方法
  22. Method writeme = pr.getWriteMethod();
  23. if (pr.getName().equals("name")) {
  24. // 执行方法
  25. writeme.invoke(p, "xiong");
  26. }
  27. if (pr.getName().equals("age")) {
  28. writeme.invoke(p, 23);
  29. }
  30. // 获取beal的get方法
  31. Method method = pr.getReadMethod();
  32. System.out.print(method.invoke(p) + " ");
  33. }
  34. }
  35. @Test
  36. public void test2() throws Exception {
  37. PropertyDescriptor pro = new PropertyDescriptor("name", Person.class);
  38. Person preson=new Person();
  39. Method  method=pro.getWriteMethod();
  40. method.invoke(preson, "xiong");
  41. System.out.println(pro.getReadMethod().invoke(preson));
  42. }
  43. }

JAVA内省(Introspector)的更多相关文章

  1. Java 内省(Introspector)深入理解

    Java 内省(Introspector)深入理解 一些概念: 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类 ...

  2. Java 内省(Introspector)和 BeanUtils

    人生若只如初见,何事秋风悲画扇. 概述 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传递数据信息, ...

  3. 聊聊Java内省Introspector

    前提 这篇文章主要分析一下Introspector(内省,应该读xing第三声,没有找到很好的翻译,下文暂且这样称呼)的用法.Introspector是一个专门处理JavaBean的工具类,用来获取J ...

  4. Java 内省 Introspector

    操纵类的属性,有两种方法 反射 内省 面向对象的编程中,对于用户提交过来的数据,要封装成一个javaBean,也就是对象 其中Bean的属性不是由字段来决定的,而是由get和Set方法来决定的 pub ...

  5. java内省Introspector

    大纲: JavaBean 规范 内省 一.JavaBean 规范 JavaBean —般需遵循以下规范. 实现 java.io.Serializable 接口. javaBean属性是具有getter ...

  6. 【小家Spring】Spring IoC是如何使用BeanWrapper和Java内省结合起来给Bean属性赋值的

    #### 每篇一句 > 具备了技术深度,遇到问题可以快速定位并从根本上解决.有了技术深度之后,学习其它技术可以更快,再深入其它技术也就不会害怕 #### 相关阅读 [[小家Spring]聊聊Sp ...

  7. 【小家Spring】聊聊Spring中的数据绑定 --- BeanWrapper以及内省Introspector和PropertyDescriptor

    #### 每篇一句 > 千古以来要饭的没有要早饭的,知道为什么吗? #### 相关阅读 [[小家Spring]聊聊Spring中的数据转换:Converter.ConversionService ...

  8. 深入理解Java:内省(Introspector)

    深入理解Java:内省(Introspector) 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传 ...

  9. Java:内省(Introspector)

    内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的字段,且 ...

随机推荐

  1. 关于Strut2内置Json插件的使用

    配置注意点: 在原有Struts2框架jar包的引入下,需要额外多加一个Json的插件包(struts2-json-plugin-2.3.7.jar) 在struts.xml配置文件中,包需要继承js ...

  2. Android 返回键双击退出程序

    /** * 菜单.返回键响应 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == K ...

  3. 简单几何(求交点) UVA 11437 Triangle Fun

    题目传送门 题意:三角形三等分点连线组成的三角形面积 分析:入门题,先求三等分点,再求交点,最后求面积.还可以用梅涅劳斯定理来做 /********************************** ...

  4. .net如何把导数据入到Excel

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  5. WPF之资源字典zz

    最近在看wpf相关东西,虽然有过两年的wpf方面的开发经验,但是当时开发的时候,许多东西一知半解,至今都是模模糊糊,框架基本是别人搭建,自己也就照着模板写写,现在许多东西慢慢的理解了,回顾以前的若干记 ...

  6. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '??????' in 'field list'

    严重: Servlet.service() for servlet jsp threw exceptioncom.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErro ...

  7. FlyCaptureProperty 摄像机属性

    An enumeration of the different camera properties that can be set via the API. Declaration enum FlyC ...

  8. people 0919

    package liu0919; public class People { private double height;// 身高 private String name;// 名字 private ...

  9. bundle的理解笔记

    Bundle是一个键值对这样一个东西.就是一个string类型的东西,对应任何类型的东西.就是用来存值的. 这里可以看到他的作用 public void onClick(View v) { Strin ...

  10. IOS第四天(5:创建备份区按钮和判断结果)

    创建备份区按钮和判断结果 /** 创建备选区按钮 */ - (void)createOptionButtons:(HMQuestion *)question { // 问题:每次调用下一题方法时,都会 ...