内省是什么?

开发框架时,经常需要使用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. Java 面向对象编程 tricks

    方法中描述了参数: 1. 构造器重载 重载构造器时,使用描述了参数的静态工厂方法名,这样做的意义何在呢?就在于为动作赋予意义,提升代码的可解释性: 传统的实例化方式: Complex fulcrumP ...

  2. 【javascript常见面试题】常见前端面试题及答案

    转自:http://www.cnblogs.com/syfwhu/p/4434132.html 前言 本文是在GitHub上看到一个大牛总结的前端常见面试题,很多问题问的都很好,很经典.很有代表性.上 ...

  3. python 编码拓展,小数据池,

    编码拓展: 1.在所有类型的编码中,编码的二进制互不识别, 2.在传输的过程中不能是万国码的二进制解码传输, 因此将unicode变为utf - 8或者变成gbk编码尤为重要; 利用encode编码为 ...

  4. 跨域问题解决方案之chrome插件

    地址: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkki ...

  5. Sentry深入

    Sentry的架构 内部架构 核心就是规则引擎以及Metadata Store:记录格式有两种,一种policy file记录授权内容,另外一种是通过命令方式进行授权:前者记录在策略文件中,保存形式是 ...

  6. 洛谷 4245 【模板】任意模数NTT——三模数NTT / 拆系数FFT

    题目:https://www.luogu.org/problemnew/show/P4245 三模数NTT: 大概是用3个模数分别做一遍,用中国剩余定理合并. 前两个合并起来变成一个 long lon ...

  7. 使用ajax技术实现简单登录操作

    1.ajax:特点在web上面通过JavaScript,使用异步的XmlHttp请求,实现无刷新的Web界面 首先:创建ajax对象 再次:向服务器端实现ajax请求 最后:回调 创建异步请求对象 & ...

  8. oracle如何查看表空间

    1.用户 查看当前用户的缺省表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select ...

  9. 【转载】Linux 进程调度时间测量

    测试Context Switch time(进程上下文切换时间) --------------------------------------------------     创建两个进程(实时进程) ...

  10. 我的MyGeneration

    话不多说,直接上代码 Interface Code: public class GeneratedGui : DotNetScriptGui { public GeneratedGui(ZeusCon ...