JavaBean的简单内省操作 以及复杂内省操作

简介: 2段简单的JavaBean代码演示了简单内省操作以及复杂内省操作  。 1、简单内省操作 package me.test; import java.lang.reflect.*; import java.

2段简单的JavaBean代码演示了简单内省操作以及复杂内省操作  。

1、简单内省操作

package me.test;
import java.lang.reflect.*;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
public class IntroSpectorTest
{
   public  static void main(String []args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
   {
    JavaBeanTest t=new JavaBeanTest() ;
    t.setX(10);
    PropertyDescriptor d=new PropertyDescriptor("X",JavaBeanTest.class);
    setProperty(t, d);
    Object val = getProperty(t, d);
    System.out.println(val);
   
   }

private static Object getProperty(JavaBeanTest t, PropertyDescriptor d)
  throws IllegalAccessException, InvocationTargetException {
 Method mr=d.getReadMethod()  ;
    Object val=mr.invoke(t);
 return val;
}

private static void setProperty(JavaBeanTest t, PropertyDescriptor d)
  throws IllegalAccessException, InvocationTargetException {
    Method mw=d.getWriteMethod()  ;
    mw.invoke(t, 5) ;
}

}
class  JavaBeanTest
{
 private int x  ;
 public void setX(int x)
 {
  this.x=x ;
 }
 public int getX()
  
 {
  return this.x ;
 }
}

 

2、复杂内省操作   BeanInfo类   Introspector类的使用

package me.test;
import java.lang.reflect.*;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
public class IntroSpectorTest
{
   public  static void main(String []args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
   {
    JavaBeanTest t=new JavaBeanTest(10) ;
    PropertyDescriptor d=new PropertyDescriptor("x",JavaBeanTest.class);
    setProperty(t, d);
    Object val = getProperty(t, d);
    System.out.println(val);
   
   }

private static Object getProperty(JavaBeanTest t, PropertyDescriptor d)
  throws IllegalAccessException, InvocationTargetException, IntrospectionException {
    BeanInfo beanInfo=Introspector.getBeanInfo(t.getClass()) ;  
 PropertyDescriptor  pt[]=beanInfo.getPropertyDescriptors() ;
  for(PropertyDescriptor tem:pt)
  {
   if(tem.getName().equals(d.getName()))
     {
             Method mr=tem.getReadMethod() ;
             Object val=mr.invoke(t) ;
             return val ;
     }
  }
 return null;

}

private static void setProperty(JavaBeanTest t, PropertyDescriptor d)
  throws IllegalAccessException, InvocationTargetException, IntrospectionException {
   
    BeanInfo beanInfo=Introspector.getBeanInfo(t.getClass() ) ; //把JavaBeanTest的对象当做JavaBean看有什么信息封装在BeanInfo中
    PropertyDescriptor [] pd=beanInfo.getPropertyDescriptors() ;  //Get All Properties From BeanInfo Class
    for(PropertyDescriptor tem:pd)
    {  
     if(tem.getName().equals(d.getName()))
     {
      Method mw=tem.getWriteMethod() ;
      mw.invoke(t, 50)  ;
      break ;
     }
    }
}

}
class  JavaBeanTest
{
 private int x  ;
 public JavaBeanTest(int x)
 {
  this.x=x ;
 }
 public void setX(int x)
 {
  this.x=x ;
 }
 public int getX()
  
 {
  return this.x ;
 }
}

 

 

 

目录
相关文章
|
1天前
|
Java 数据库
JAVA对象和类
JAVA对象和类
7 0
|
1月前
|
存储 Java 对象存储
Java对象和类
Java对象和类
15 1
|
2月前
|
Java Spring
Spring注入类的两种形式
Spring注入类的两种形式
49 0
Spring注入类的两种形式
|
3月前
|
监控 安全 Java
Java反射:深入了解动态类操作
Java反射:深入了解动态类操作
70 0
|
8月前
|
设计模式 缓存 Java
Java反射(反射与代理设计模式、反射与Annotation、自定义Annotation、反射整合工厂设计模式和代理设计模式)
1.反射与代理设计模式,2.反射与Annotation,3.自定义Annotation,4.Annotation整合工厂设计模式和代理设计模式
45 0
|
存储 Java Spring
Spring框架中注入集合对象
你好看官,里面请!今天笔者讲的是在Spring框架中关于注入集合对象的用法(有示例!全网最详细!!) 不懂可以在评论区留言,我看到会及时回复。 注意:本文仅用与学习参考,不可用于商业用途。
785 3
Spring框架中注入集合对象
|
Java 数据库
内省机制(操作javaBean的信息)
内省机制(操作javaBean的信息)
94 0
内省机制(操作javaBean的信息)
|
存储 Java
javaBean内省类【javaBean、BeanInfo、Introspector、PropertyDescriptor】
javaBean内省类【javaBean、BeanInfo、Introspector、PropertyDescriptor】
176 0
javaBean内省类【javaBean、BeanInfo、Introspector、PropertyDescriptor】
|
Java 数据库连接
(反射+内省机制的运用)处理jdbc的结果集
(反射+内省机制的运用)处理jdbc的结果集
82 0