import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
public class IntrospectorDemo { public static void main(String[] args) {
// TODO Auto-generated method stub
IntrospectorDemo id=new IntrospectorDemo();
try {
id.test1();
id.test2();
id.test3();
id.test4();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public class Person
{
public Person(){
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
private String name;
private int age;
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;
} } //获得person类的所有bean属性 public void test1() throws Exception{
BeanInfo info = Introspector.getBeanInfo(Person.class);
PropertyDescriptor[] pds = info.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
System.out.println(pd.getPropertyType()+" "+pd.getName());
}
}
//获得Person(不包含Object类)的所有bean属性 public void test2() throws Exception{
BeanInfo info = Introspector.getBeanInfo(Person.class,Object.class);
PropertyDescriptor[] pds = info.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
System.out.println(pd.getPropertyType()+" "+pd.getName());
}
}
//设置Person的age属性 public void test3() throws Exception{
Person p = new Person();
PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);
Method method = pd.getWriteMethod();
method.invoke(p, 20);
System.out.println(p.getAge());
}
//调用Person的getAge() public void test4() throws Exception{
Person p = new Person("xiazdong",30);
PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);
Method method = pd.getReadMethod();
System.out.println(method.invoke(p, null));
} }

Introspector的更多相关文章

  1. 解决java.beans.Introspector导致的内存泄漏

    解决方案: 在WEB.XML 中配置监听器: <listener> <listener-class> org.springframework.web.util.Introspe ...

  2. 内省(introspector)------>JavaBean

    内省(introspector)------>JavaBean    1.问什么要学内省?        开发框架时,经常需要Java对象的属性来来封装程序的数据,每次使用反射技术完成此操作过于 ...

  3. 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性

      本文转载自:http://blog.sina.com.cn/s/blog_5d65a16901011kom.html 关键字:内省.JavaBean.PropertyDescriptor类.Int ...

  4. 内省(Introspector)

    内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法 目的:主要用于传递数据信息,这种类中的方法主要用于访问私有的字段(且方法名符合某种命名规则) p ...

  5. [新手学Java]使用内省(Introspector)操作JavaBean属性

    获取类bean中的所有属性: @Test //获取类bean中的所有属性 public void test1() throws Exception{ BeanInfo info = Introspec ...

  6. JAVA内省(Introspector)

    什么是Java内省:内省是Java语言对Bean类属性.事件的一种缺省处理方法. Java内省的作用:一般在开发框架时,当需要操作一个JavaBean时,如果一直用反射来操作,显得很麻烦:所以sun公 ...

  7. java的内省(introspector)

    package com.wzh.test.introspector; import java.beans.BeanInfo; import java.beans.IntrospectionExcept ...

  8. java内省机制Introspector

    访问JavaBean属性的两种方式 1)直接调用bean的setXXX或getXXX方法: 2)通过内省技术访问(java.beans包提供了内省的API),内省技术访问也提供了两种方式: a)通过P ...

  9. Introspector(内省)简单演示样例 与 简单应用

    简单演示样例: package com.asdfLeftHand.test; import java.beans.BeanDescriptor; import java.beans.BeanInfo; ...

  10. Java 内省 Introspector

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

随机推荐

  1. 常用Java开源库(新手必看)

    Jakarta common: Commons LoggingJakarta Commons Logging (JCL)提供的是一个日志(Log)接口(interface),同时兼顾轻量级和不依赖于具 ...

  2. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

  3. Insert Data with C# Driver

    https://docs.mongodb.com/getting-started/csharp/insert/ OverView You can use the InsertOneAsync meth ...

  4. hpuoj--校赛--2015年的第一场雪(暴力)

    问题 D: 感恩节KK专场--2015年的第一场雪 时间限制: 1 Sec  内存限制: 128 MB 提交: 865  解决: 76 [提交][状态][讨论版] 题目描述 下雪了,KK学长站在三教门 ...

  5. 22.允许重复的容器(unordered_multiset)

    #include <string> #include <iostream> #include <unordered_set> using namespace std ...

  6. BZOJ 3223 Splay区间翻转

    思路: 区间翻转的裸题 终于tm理解splay了-- //By SiriusRen #include <cstdio> #include <cstring> #include ...

  7. prezi,mfc,toefl,java

    1 用prezi做ppt然后讲 2 用mfc把算法封起来 3 做tpo,背单词 4 写java 哪个任务都很难办.而且脚还没好.

  8. <Sicily>Funny Game

    一.题目描述 Two players, Singa and Suny, play, starting with two natural numbers. Singa, the first player ...

  9. MVC中几种常用的ActionResult

    一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等 ...

  10. [NOIP2015提高组]运输计划

    题目:BZOJ4326.洛谷P2680.Vijos P1983.UOJ#150.codevs4632.codevs5440. 题目大意:有一棵带权树,有一些运输计划,第i个运输计划从ai到bi,耗时为 ...