内省是什么?

开发框架时,经常需要使用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. MyISAM引擎的特点及优化方法

    1.什么是MyISAM引擎?MyISAM引擎是MySQL关系数据库管理系统的默认存储引擎(MySQL5.5.5以前),这种MySQL的表存储结构从旧的ISAM代码扩展出许多有用的功能.在存储的时候,每 ...

  2. C语言编译全过程

    编译的概念:编译程序读取源程序(字符流),对之进行词法和语法的分析,将高级语言指令转换为功能等效的汇编代码,再由汇编程序转换为机器语言,并且按照操作系统对可执行文件格式的要求链接生成可执行程序.    ...

  3. 【剑指offer】以o(1)复杂度删除啊链表的节点,C++实现(链表)

    0.简介       本文是牛客网<剑指offer>刷题笔记. 1.题目       在O(1)时间内删除链表节点. 2.思路         前提条件:删除的节点在链表上:边界条件:链表 ...

  4. Why getting this error “django.db.utils.OperationalError: (1050, ”Table 'someTable' already exists“)”

    0down votefavorite   I am getting error like django.db.utils.OperationalError: (1050, "Table 's ...

  5. Directx 9 VS2015环境搭建

    安装好Directx9 sdk和vs2015后 打开vs,新建项目 --> c++项目  -->win32控制台应用程序-->空项目 创建项目后,右键项目属性, 包含目录 D:\Pr ...

  6. 如何测试远端TCP和UDP端口是否开放

    项目遇到问题时首先排查网络是否正常是一个重要的方面.遇到很多次,同事找我解决问题,最后发现却是IP或端口不通的问题.然而就是这么个简单的问题,对方却花费了甚至一天的时间排查原因. 现在大部分项目都是用 ...

  7. Poj 3287 Catch That Cow(BFS)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  8. 在Mac系统下如何恢复SourceTree全局忽略的文件

    在家目录“~”下编辑 “.gitignore_global ” 文件即可:vim  .gitignore_global

  9. DCloud-HBulder:杂项

    ylbtech-DCloud-HBulder:杂项 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   ...

  10. rem怎么计算

    px:相对长度单位.像素px是相对于显示器屏幕分辨率而言的 em:相对单位,继承父节点(层层继承,传递)基准点为父节点字体的大小,如果自身定义了font-size按自身来计算(浏览器默认字体是16px ...