1.请使用sun 内省api得到某一个bean的所有属性,并操作bean的其中一个属性(给属性赋值,以及得到属性的值) 。
来源:互联网 发布:淘宝用微信怎么支付 编辑:程序博客网 时间:2024/06/07 00:19
package com.hbsi.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.locale.converters.DateLocaleConverter;
import org.junit.Test;
public class Demo1 {
@Test
public void test1() throws IllegalAccessException, InvocationTargetException{
Person p=new Person();
BeanUtils.setProperty(p,"name","张三");
System.out.println(p.getName());
}
//beanutils工具对基本数据类型可以自动转换类型
@Test
public void test2() throws IllegalAccessException, InvocationTargetException{
//自定义转换器
ConvertUtils.register(new Converter(){
@Override
public Object convert(Class type, Object value) {
if(value==null){
return null;
}
if(!(value instanceof String)){
throw new ConversionException("只能转String数据");
}
String s=(String)value;
if(s.trim().equals("")){
return null;
}
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try {
Date d=sdf.parse(s);
return d;
} catch (ParseException e) {
// TODO Auto-generated catch block
throw new ConversionException("转换错误");
}
}
}, Date.class);
String name="张三";
String age="23";
String birthday=" ";
Person p=new Person();
BeanUtils.setProperty(p,"name",name);
BeanUtils.setProperty(p,"age",age);
BeanUtils.setProperty(p,"birthday",birthday);
System.out.println(p.getName()+"..."+p.getAge()+"...");
}
//使用beanUtils中的转换器完成数据转换
@Test
public void test3() throws IllegalAccessException, InvocationTargetException{
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person p=new Person();
BeanUtils.setProperty(p,"birthday"," ");
//System.out.println(p.getBirthday());
}
}
/*class MyConver implements Converter{
@Override
public Object convert(Class arg0, Object arg1) {
// TODO Auto-generated method stub
return null;
}
}*/
package com.hbsi.beanutils;
import java.util.Date;
public class Person {
private String name;
private int age;
private Date birthday;
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAb(){
return null;
}
}
package com.hbsi.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;
public class Demo1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
//通过内省获取person bean的所有属性
@Test
public void test1() throws Exception{
BeanInfo bi=Introspector.getBeanInfo(Person.class,Object.class);
PropertyDescriptor[] pds=bi.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
String name=pd.getName();
System.out.println(name);
}
}
//通过内省给person的name属性赋值:abc setName("abc")
@Test
public void test2() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Person p=new Person();
BeanInfo bi=Introspector.getBeanInfo(Person.class);
PropertyDescriptor[] pds=bi.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
String name=pd.getName();
if(name.equals("name")){
Method m=pd.getWriteMethod();
m.invoke(p, "abc");
}
}
System.out.println(p.getName());
}
//通过PropertyDescriptor类操作Bean的属性 name属性赋值:ffff setName("abc")
@Test
public void Test3() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Person p=new Person();
PropertyDescriptor pd=new PropertyDescriptor("name", p.getClass());
Method m=pd.getWriteMethod();
m.invoke(p,"ffff");
System.out.println(p.getName());
}
@Test
public void Test4() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Person p=new Person();
p.setName("abcdef");
PropertyDescriptor pd=new PropertyDescriptor("name",p.getClass());
Method m=pd.getReadMethod();
String str=(String)m.invoke(p, null);
System.out.println(str);
}
}
package com.hbsi.introspector;
public class Person {
private String name;
private 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 getAb(){
return null;
}
}
- 1.请使用sun 内省api得到某一个bean的所有属性,并操作bean的其中一个属性(给属性赋值,以及得到属性的值) 。
- 请使用sun 内省api得到某一个bean的所有属性,并操作bean的其中一个属性(给属性赋值,以及得到属性的值
- 使用内省api操作bean的属性
- Java中使用内省操作Bean的所有属性
- 使用内省(Introspector)操作bean属性
- 给Bean的属性赋值的好方法
- BeanUtils操作Bean的属性
- BeanUtils操作bean的属性
- 使用beanutils操作bean的属性
- 使用BeanUtils操作Bean的各种属性
- spring 用bean的方法、属性赋值另外一个bean的属性
- 相同属性的Bean相互赋值
- 遍历一个实例的所有属性,得到属性的名称和值
- 如何遍历一个实例的所有属性,得到属性的名称和值
- 如何遍历一个实例的所有属性,得到属性的名称和值
- 如何遍历一个实例的所有属性,得到属性的名称和值
- bean的scope属性
- bean的scope属性
- priority_queue的用法
- 软件工程管理——《人月神话的读书笔记》(chapter2~chapter10)
- Android 音频系统
- Debug Error! DAMAGE:after Normal Block (#Xxxx) 问题
- Log4j使用总结
- 1.请使用sun 内省api得到某一个bean的所有属性,并操作bean的其中一个属性(给属性赋值,以及得到属性的值) 。
- linux bash shell set 命令详解
- Android 进程通信之AIDL
- 2012-9-18
- 强制退出Mac程序的六种方法
- MyEClipse出现空格和换行字符的解决方案
- Java基础加强之内省(introspector)
- 又见一帘幽梦
- 地图市场三足鼎立:诺基亚官方确认与亚马逊合作