什么是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. c++工程vs导入工程时发生LNK1207

    I have installed VS 2012 , but i have VS 2010 also. After I open  VS 2010 projects with VS 2012 and  ...

  2. git学习 远程仓库02

    使用远程仓库: 查看当前远程库://克隆后,至少有一个名为 origin 的远程库,Git 默认使用这个名字来标识你所克隆的原始仓库 git remote -v: 并显示所有远程库的地址: 添加远程仓 ...

  3. onclientclick和onclick区别

    OnClientClick是客户端脚本,一般使用javascript,在客户端,也就是IE中运行,点击后马上执行OnClick是服务器端事件处理函数,使用C#或者vb.net,在服务器端,也就是IIS ...

  4. ural 1153. Supercomputer

    1153. Supercomputer Time limit: 2.0 secondMemory limit: 64 MB To check the speed of JCN Corporation ...

  5. quick cocos 暂停场景

    local MainScene = class("MainScene", function() return display.newScene("MainScene&qu ...

  6. BZOJ3251 : 树上三角形

    BZOJ AC1000题纪念~~~ 将x到y路径上的点权从小到大排序 如果不存在b[i]使得b[i]+b[i+1]>b[i+2]则无解 此时b数列增长速度快于斐波那契数列,当达到50项时就会超过 ...

  7. 精通CSS :nth-child伪类

    :nth-child 基本用法 :nth-child:nth-child(8) 选中第8个子元素 li:nth-child(8) span { background-color: #298EB2; b ...

  8. 我的第一个 Mono for Android 应用

    创建 Mono for Android 应用 打开 MonoDevelop , 选择新建解决方案, 左边的分类选择 "Mono for Android" , 右边选择 " ...

  9. [leetCode][016] Add Two Numbers

    [题目]: You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  10. POJ 2948 Martian Mining(DP)这是POJ第200道,居然没发现

    题目链接 两种矿石,Y和B,Y只能从从右到左,B是从下到上,每个空格只能是上下或者左右,具体看图.求左端+上端最大值. 很容易发现如果想最优,分界线一定是不下降的,分界线上面全是往上,分界线下面都是往 ...