内省(Introspector)

时间:2022-09-03 22:09:38

内省(Introspector) 是Java 语言对 JavaBean 类属性、事件的一种缺省处理方法

目的:主要用于传递数据信息,这种类中的方法主要用于访问私有的字段(且方法名符合某种命名规则)

package day02.introspector;

public class Person {
public String name;
public 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;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String high;
public int getAb() {
return age;
}
}

//使用内省操作bean的属性

package day02.introspector;
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;
//使用内省操作bean的属性
public class Demo1 {
//得到bean的所有属性
@Test
public void Test() throws IntrospectionException{
BeanInfo info= Introspector.getBeanInfo(Person.class,Object.class);//拿到bean自己的属性
PropertyDescriptor[] psd=info.getPropertyDescriptors();//得到属性描述器
for(PropertyDescriptor pd:psd){
System.out.println(pd.getName());
}
} //操作bean的属性:age
@Test
public void Test2() throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Person p=new Person();
PropertyDescriptor pd=new PropertyDescriptor("age",Person.class);
//得到属性的写方法,为属性赋值
Method method=pd.getWriteMethod();
method.invoke(p,23);
//获得属性值
method=pd.getReadMethod();
System.out.println(method.invoke(p, null));
// System.out.println(p.getAge());
} }

以上代码可以运行后体验下

//高级点的操作:bean的操作属性类型
@Test
public void Test3 () throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Person p=new Person();
PropertyDescriptor pd=new PropertyDescriptor("age",Person.class);
System.out.println(pd.getPropertyType());
}

get set数据

注册日期转换器

@Test
public void test2() throws IllegalAccessException, InvocationTargetException {
String name = "aaaa";
String high = "232";
String age = "34";
String birthday = "1995-12-10";
// String today="1996-12-10";
// ConvertUtils.register(new DateLocaleConverter(), Date.class);//方法之二:有bug,不要用!
Person p = new Person();
// 为了让日期赋到bean的birthday的属性上。我们给beanuntils注册一个日期转换器
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class arg0, Object value) {
if (value == null) {
return null;
}
if (!(value instanceof String)) {
System.out.println("no");
throw new ConversionException("只转String");
}
String str = (String) value;
if (str.trim().equals("")) {
return null;
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);// 异常链不能断
}
}
}, Date.class);
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "high", high);// 只支持8种基本类型自动转型
BeanUtils.setProperty(p, "age", age);// 只支持8种基本类型自动转型
BeanUtils.setProperty(p, "birthday", birthday);//通过转化器成功 System.out.println(p.getName());
System.out.println(p.getHigh());
System.out.println(p.getAge());
System.out.println(p.getBirthday());
// Date date=p.getToday();
// System.out.println(date.toLocaleString());
}

用map

//用map来添数据
@Test
public void test5() throws IllegalAccessException, InvocationTargetException{
Map map=new HashMap();
map.put("name", "aaaa");
map.put("high", "123");
map.put("age", "23");
map.put("birthday", "1995-12-10");
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person p=new Person();
BeanUtils.populate(p, map);
System.out.println(p.getName());
System.out.println(p.getHigh());
System.out.println(p.getAge());
System.out.println(p.getBirthday());
}

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

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

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

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

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

  3. Java:内省(Introspector)

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

  4. JavaBeans与内省(Introspector)

    JavaBean与Introspector 反射和内省操作很多时候都是在以后要做框架的时候作用非常大.    现在你学的是面向对象编程,即:你所写代码都能够找到对应的类或接口,找到具体的方法写出对应的 ...

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

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

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

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

  7. Java 内省(Introspector)和 BeanUtils

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

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

    本文转载自:https://www.cnblogs.com/peida/archive/2013/06/03/3090842.html 一些概念: 内省(Introspector) 是Java 语言对 ...

  9. 聊聊Java内省Introspector

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

随机推荐

  1. C#与C++的发展历程第四 - C#6的新时代

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  2. Combination Sum

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  3. 串口 COM口 TTL RS-232 RS-485 区别 释疑

    Point: 1.串口.COM口是指的物理接口形式(硬件).而TTL.RS-232.RS-485是指的电平标准(电信号). 2.接设备的时候,一般只接GND RX TX.不会接Vcc或者+3.3v的电 ...

  4. NYOJ-756重建二叉树

    重建二叉树 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 题目很简单,给你一棵二叉树的后序和中序序列,求出它的前序序列(So easy!). 输入 输入有多组数据(少于 ...

  5. ubuntu14.04 安装tar.gz文件

    UBUNTU14.04 中tar.gz安装方法 # 是root用的,如果想一直用就要先root设置密码sudo passwd root.$ 一般用户 root@big-System-Product-N ...

  6. Servlet3.0新特性使用详解

    可插拔的Web框架 几乎所有基于Java的web框架都建立在servlet之上.现今大多数web框架要么通过servlet.要么通过Web.xml插入.利用标注(Annotation)来定义servl ...

  7. 2017-4-25/设计缓存(LFU)

    1. 恒定缓存性能有哪些因素? 命中率.缓存更新策略.缓存最大数据量. 命中率:指请求缓存次数和缓存返回正确结果次数的比例.比例越高,缓存的使用率越高,用来衡量缓存机智的好坏和效率.如果数据频繁更新, ...

  8. Mybatis了解(配置)

    Mybatis是一个基于jdbc映射框架.它跟hibernate一样都是对数据库进行操作的.Mybatis 它是通过配置xml或者是注解来进行映射的配置,最后实现操作接口与pojo来操作数据库. 因此 ...

  9. Swift中if与switch语句使用一例

    在Swift中相同的条件处理有if和switch两个语句,我们如何取舍呢? 一种情况下我们只在乎x是否在一个范围内,但并不关心x是否穷尽!换句话说不关心在满足范围条件的其他情况下,此时我们可以考虑用i ...

  10. OSU! on tree

    dsu on tree 好吧,这个毒瘤...... 树剖和启发式合并的杂合体. 用于解决静态子树问题,复杂度O(nlogn * insert时间) 因为dsu是并查集的意思所以算法名字大概就是什么树上 ...