11 java 王少飞-Java SE 加强 内省小节

内省(Introspector)
     为什么要学内省?
     开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都使用反射技术完成此类操作过于麻烦,所以sun公司开发了一套API,专门用于操作java对象的属性。
     内省是 Java 语言对 Bean 类属性的一种缺省处理方法。例如类 A 中有属性 name, 可以通过 getName,setName 来得到其值或者设置新的值。通过 getName/setName 来访问 name 属性,这是默认的规则。 Java 中提供了一套 API 来访问某个属性的 getter/setter 方法。
     一般的做法是通过类 Introspector 来获取某个对象的 BeanInfo 信息,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后通过反射机制来调用这些方法
 
    内省访问JavaBean属性的两种方式:
    1)通过PropertyDescriptor类操作Bean的属性
    2)通过Introspector类获得Bean对象的 BeanInfo,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后通过反射机制来调用这些方法。
 
实例--------------------------

package com.hbsi.homework;

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 org.junit.Test;

public class IntrospectorDemo {
 public static void main(String[] args) {
  // TODO Auto-generated method stub

 }
 //通过内省获取person bean的所有属性
 @Test
 public void test1() throws IntrospectionException{
  BeanInfo bi=Introspector.getBeanInfo(Student.class,Object.class);
        PropertyDescriptor[] pds=bi.getPropertyDescriptors();
  for(PropertyDescriptor pd:pds){
   String name=pd.getName();
   System.out.println(name);
  }
 }
 //通过内省给person的name属性赋值:abc   setName("abc")
 @Test
 public void test2() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
  Student s=new Student();
  BeanInfo bi=Introspector.getBeanInfo(Student.class);
  PropertyDescriptor[] pds=bi.getPropertyDescriptors();
  for(PropertyDescriptor pd:pds){
   String name=pd.getName();
   if(name.equals("name")){
    Method m=pd.getWriteMethod();
    m.invoke(s, "abc");
   }
  }
  System.out.println(s.getName());
 }
 //通过PropertyDescriptor类操作Bean的属性  name属性赋值:aaaa   setName("aaaa")
 @Test
 public void Test3() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
  Student s=new Student();
  PropertyDescriptor pd=new PropertyDescriptor("name", s.getClass());
  Method m=pd.getWriteMethod();
  m.invoke(s,"aaaa");
  System.out.println(s.getName());
 }
 @Test
 public void Test4() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
  Student s=new Student();
  s.setName("abcdef");
  PropertyDescriptor pd=new PropertyDescriptor("name",s.getClass());
  Method m=pd.getReadMethod();
  String str=(String)m.invoke(s, null);
  System.out.println(str);
 }
}

 
  Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单、易用的API操作Bean的属性——BeanUtils
  Beanutils工具包的常用类:
  BeanUtils
  PropertyUtils
  ConvertUtils.regsiter(Converter convert, Class clazz)
  自定义转换器
 
实例--------------------------

package com.hbsi.homework;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;

public class BeanutilsDemo {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
    }
 @Test
 public void test1() throws IllegalAccessException, InvocationTargetException{
  Student s=new Student();
  BeanUtils.setProperty(s,"name","张三");
  System.out.println(s.getName());
 }
 //使用beanUtils中的转换器完成数据转换
 @Test
 public void test2() throws IllegalAccessException, InvocationTargetException{
  ConvertUtils.register(new DateLocaleConverter(), Date.class);
  Student s=new Student();
  BeanUtils.setProperty(s,"birthday","1992-10-09");
  System.out.println(s.getBirthday().toLocaleString());
 }
 @Test
 public void test3() throws IllegalAccessException, InvocationTargetException{
  //自定义转换器
  ConvertUtils.register(new Converter(){
   public Object convert(Class type, Object value) {
    if(value==null){
     return null;
    }
    if(!(value instanceof String)){
     throw new ConversionException("只能转String数据");
    }
    String s=(String)value;
    if(s.trim().equals("")){
     return null;
    }
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    try {
     Date d=sdf.parse(s);
     return d;
    } catch (ParseException e) {
     // TODO Auto-generated catch block
     throw new ConversionException("转换错误");
    }   
   }  
  }, Date.class);
  String name="张三";
  String age="23";
  String birthday="1992-10-09";
  
  Student s=new Student();
  BeanUtils.setProperty(s,"name",name);
  BeanUtils.setProperty(s,"age",age);
  BeanUtils.setProperty(s,"birthday",birthday);
  
  System.out.println("姓名:"+s.getName()+"     年龄:"+s.getAge()+"    出生日期:"+s.getBirthday().toLocaleString());
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值