内省是什么?

开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都是用反射技术完成此类操作过于麻烦,所以sun公司开发了一套API,专门用于操作Java对象的属性。

什么是Java对象的属性和属性的读写方法?

内省访问JavaBean属性的两种方式:

1.通过ProperityDescriptor类操作Bean的属性;

2.通过Introspector类获得Bean对象的BeanInfo,然后通过BeanInfo来获取属性的描述器(PropertyDescriptor),通过这个属性描述器就可以获得某个属性对应的getter/setter方法,然后通过反射机制来调用这些方法。

在类中声明成员变量(private String name;)只能是叫做成员变量或者叫做字段,只有当声明了该字段的set和get方法时才能成这个成员变量为属性。

下面看使用jdkapi中的内省调用类里面的属性。

 package cn.itcast.instrospector;

 import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method; import org.junit.Test; public class Demo1 { //通过内省api操作bean的name属性
@Test
public void test1() throws Exception{
Student s = new Student(); PropertyDescriptor pd = new PropertyDescriptor("name",Student.class);
Method method = pd.getWriteMethod();
method.invoke(s, "flx");
//System.out.println(s.getName());
method = pd.getReadMethod();
String result = (String) method.invoke(s, null);
System.out.println(result);
} //操作bean的所有属性
@Test
public void test2() throws Exception{
BeanInfo info = Introspector.getBeanInfo(Student.class);
PropertyDescriptor pds[] = info.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
System.out.println(pd.getName());
}
}
}
 package cn.itcast.instrospector;

 public class Student {
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

下面看使用BeanUtils操作属性

 package cn.itcast.beanutils;

 import java.lang.reflect.InvocationTargetException;
import java.util.Date; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter; public class Demo1 {
public static void main(String [] args) throws IllegalAccessException, InvocationTargetException{
String name = "fix";
String password = "123";
String age = "23";
String email = "flx@sina.com";
String birthday = "1990-09-09"; Student s = new Student(); ConvertUtils.register(new DateLocaleConverter(), Date.class);
BeanUtils.setProperty(s, "name", name);
BeanUtils.setProperty(s, "password", password);
BeanUtils.setProperty(s, "age", age);
BeanUtils.setProperty(s, "email", email);
BeanUtils.setProperty(s, "birthday", birthday);
System.out.println(s.getEmail());
System.out.println(s.getBirthday());
} }

这里第21行注册了一个转化器,使日期可以转换成当地日期。下面是Student类

 package cn.itcast.beanutils;

 import java.util.Date;

 public class Student {
private String name;
private String password;
private String email;
private int age;
private Date birthday;
public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

但是,如果我们输入的数据类型没有对应的转化器,我们就要自己写一个转换器。

下面看如何写一个转换器。

 package cn.itcast.beanutils;

 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.converters.DateConverter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter; public class Demo1 {
public static void main(String [] args) throws IllegalAccessException, InvocationTargetException{
String name = "fix";
String password = "123";
String age = "23";
String email = "flx@sina.com";
String birthday = "1990-09-09"; Student s = new Student(); //自己设计转换器
ConvertUtils.register(new Converter(){
public Object convert(Class type,Object value){//"1980-09-09"
if(value==null){
return null;
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse((String) value);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ConversionException(e);
}
return date;
}
}, Date.class);
BeanUtils.setProperty(s, "name", name);
BeanUtils.setProperty(s, "password", password);
BeanUtils.setProperty(s, "age", age);
BeanUtils.setProperty(s, "email", email);
BeanUtils.setProperty(s, "birthday", birthday);
System.out.println(s.getEmail());
System.out.println(s.getBirthday());
} }

以上就是内省的一些使用技巧,这里在能用到BeanUtils是尽量用BeanUtils。

JavaWeb_内省(Instrospector)的更多相关文章

  1. Java内省机制

    转自: https://blog.csdn.net/hahalzb/article/details/5972421 1.java内省机制其实通俗的理解为,对自身的进行一个扫描,这个扫描的对象就是我们普 ...

  2. JAVA中反射机制五(JavaBean的内省与BeanUtils库)

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

  3. QT内省机制、自定义Model、数据库

    本文将介绍自定义Model过程中数据库数据源的获取方法,我使用过以下三种方式获取数据库数据源: 创建 存储对应数据库所有字段的 结构体,将结构体置于容器中返回,然后根据索引值(QModelIndex) ...

  4. java 深入技术八(内省)

    1. javabean的软件设计思想 2.内省:封装了java反射,提供直接操作属性的Setter和getter方法的方法 3.核心API:BeanInfo java 的描述信息,Introspect ...

  5. jsp 以及javabean内省技术

    l JSP l JavaBean及内省 l EL表达式 1.1 上次课内容回顾 会话技术: Cookie:客户端技术.将数据保存在客户端浏览器上.Cookie是有大小和个数的限制. Session:服 ...

  6. 【Java EE 学习 23】【log4j的使用】【ant的使用】【内省】

    一.ant下载地址:http://ant.apache.org/bindownload.cgi  二.log4j下载地址:http://logging.apache.org/log4j/2.x/dow ...

  7. java内省机制及PropertyUtils使用方法

    背景 一般情况下,在Java中你可以通过get方法轻松获取beans中的属性值.但是,当你事先不知道beans的类型或者将要访问或修改的属性名时,该怎么办?Java语言中提供了一些像java.bean ...

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

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

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

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

随机推荐

  1. bzoj 4710 分特产

    有 $n$ 个人,$m$ 种物品,每种物品有 $a_i$ 个,求每个人至少分到一个的方案数 $n,m,a_i \leq 2000$ sol: 比上一个题简单一点 还是考虑容斥 每个人至少分到一个 = ...

  2. C#进阶之路(五):Linq初识

    关于LINQ的文章,网上有很多,所以这篇文章我主要是总结下我自己的学习心得. 首先需要先了解的相关技术 1.隐式类型.匿名类型.对象初始化器 1)隐式类型,使用var关键字创建,C#编译器会根据用于初 ...

  3. 市场上 MLCC 226 电容现象

    市场上 MLCC 226 电容现象 三星 X7R 1206 没有 16V 也有人在卖. Y5V 当 X7R 卖. X5R 当 X7R 卖. 薄电容当厚的电容卖.

  4. CentOS7 yum安装mysql5.5/5.6并初始化

    https://blog.csdn.net/petrel2015/article/details/78822466 下载MySQL yum仓库文件 首先根据官网给出的建议,下载MySQL的仓库文件 h ...

  5. C++空类大小

    class a {};class b{};class c:public a{ virtual void fun()=0;};class d:public b,public c{}; 类a,b明明是空类 ...

  6. Azure上采用Powershell从已有的VHD创建VM

    刚刚的一篇Blog采用Json Template的方式从已有的VHD创建了一台新的VM.由于Json Template封装的比较好,可以改的内容不多. 下面将介绍通过用Powershell来从已有的V ...

  7. bmp图片格式及读取

    C++读取bmp图片的例子 #include <windows.h> #include <stdio.h> #include <stdlib.h> #include ...

  8. 网页效果分析 VCD分解

    VCD分解分为三部分: 1. view 视觉                   HTML + CSS 基本界面模板 2. controller 控制            javascript  内 ...

  9. 机器学习:集成学习(Ada Boosting 和 Gradient Boosting)

    一.集成学习的思路 共 3 种思路: Bagging:独立的集成多个模型,每个模型有一定的差异,最终综合有差异的模型的结果,获得学习的最终的结果: Boosting(增强集成学习):集成多个模型,每个 ...

  10. 专题练习HDU题集 图论

    [图论01]最短路 Start Time : 2018-01-02 12:45:00    End Time : 2018-01-23 12:45:00 Contest Status : Runnin ...