JavaWeb_内省(Instrospector)
2024-08-31 10:12:56
内省是什么?
开发框架时,经常需要使用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。
最新文章
- poj 1112
- 用VBox虚拟机安装Android 屏幕90度翻转竖屏设置
- 4. Linux常用命令
- 关于java 中文乱码问题 自己的一点解决方案
- Big Number--hdu1018(数学)
- win7系统安装FAQ
- sublime3使用
- java面试题之ssh
- VS2010 和VS2012 的程序在XP上运行的方法
- Impala:新一代开源大数据分析引擎
- scroll事件实现监控滚动条并分页显示示例(zepto.js )
- 数据库导入Excel数据的简易方法
- [Swift]LeetCode657. 机器人能否返回原点 | Robot Return to Origin
- 日志入库-log4j-mysql连接中断问题
- MySql cmd下的学习笔记 —— 有关视图的操作(建立表)
- MySQL登录报错ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
- 20165220 Java第五周学习总结
- rdesktop方法(Linux to Windows)
- EntityFramework 多数据库链接,MySql,SqlServer,Oracel等
- SQLite加密方式 [转]