当前位置: 首页 > news >正文

互联网网站建设价格淘宝关键词搜索量排名

互联网网站建设价格,淘宝关键词搜索量排名,大学生网站开发,wordpress wp_footer在哪里定义BeanWrapper 是 Spring 框架中的一个接口,它提供了一种方式来设置和获取 JavaBean 的属性。JavaBean 是一种特殊的 Java 类,遵循特定的编码约定(例如,私有属性和公共的 getter/setter 方法),通常用于封装数…

BeanWrapper 是 Spring 框架中的一个接口,它提供了一种方式来设置和获取 JavaBean 的属性。JavaBean 是一种特殊的 Java 类,遵循特定的编码约定(例如,私有属性和公共的 getter/setter 方法),通常用于封装数据

主要功能
1.属性访问:
        BeanWrapper 允许以统一的方式访问 JavaBean 对象的属性,包括简单属性、复杂类型属性、集合和数组等。它支持通过属性路径(property path)来访问嵌套对象的属性,例如 person.address.streetName。
2.类型转换:
        BeanWrapper 内置了对常见类型的转换支持,可以自动处理不同数据类型的转换。
可以注册自定义的 PropertyEditor 或使用 ConversionService 来实现更复杂的类型转换逻辑。
3.属性编辑器:
        可以为特定的数据类型注册 PropertyEditor,这允许在从字符串到对象或反之的转换过程中进行定制化处理。
4.错误处理:
        当尝试设置非法值或访问不存在的属性时,BeanWrapper 可以捕获并报告这些错误。

访问简单属性 

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;public class BeanWrapperExample {public static void main(String[] args) {// 创建一个目标对象Person person = new Person();// 创建 BeanWrapper 实例BeanWrapper beanWrapper = new BeanWrapperImpl(person);// 设置属性值beanWrapper.setPropertyValue("name", "John Doe");beanWrapper.setPropertyValue("age", 30);// 获取属性值String name = (String) beanWrapper.getPropertyValue("name");int age = (Integer) beanWrapper.getPropertyValue("age");System.out.println("Name: " + name);System.out.println("Age: " + age);}
}class Person {private String name;private int age;// 必须提供 getter 和 setter 方法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;}
}

 访问嵌套属性

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;public class NestedPropertyAccess {public static void main(String[] args) {Person person = new Person();Address address = new Address();person.setAddress(address);BeanWrapper beanWrapper = new BeanWrapperImpl(person);// 设置嵌套属性beanWrapper.setPropertyValue("address.street", "123 Main St");beanWrapper.setPropertyValue("address.city", "Springfield");// 获取嵌套属性String street = (String) beanWrapper.getPropertyValue("address.street");String city = (String) beanWrapper.getPropertyValue("address.city");System.out.println("Street: " + street); // 输出: Street: 123 Main StSystem.out.println("City: " + city);     // 输出: City: Springfield}
}class Person {private String name;private int age;private Address address;// Getters and Setterspublic 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 Address getAddress() { return address; }public void setAddress(Address address) { this.address = address; }
}class Address {private String street;private String city;// Getters and Setterspublic String getStreet() { return street; }public void setStreet(String street) { this.street = street; }public String getCity() { return city; }public void setCity(String city) { this.city = city; }
}

自动类型转换

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;public class TypeConversion {public static void main(String[] args) {Person person = new Person();BeanWrapper beanWrapper = new BeanWrapperImpl(person);// 设置属性值,自动类型转换beanWrapper.setPropertyValue("age", "30"); // 字符串 "30" 转换为 int 30// 获取属性值int age = (Integer) beanWrapper.getPropertyValue("age");System.out.println("Age: " + age); // 输出: Age: 30}
}class Person {private String name;private int age;// Getters and Setterspublic 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; }
}

注册自定义 PropertyEditor

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.beans.PropertyEditorSupport;public class CustomPropertyEditor {public static void main(String[] args) {Person person = new Person();BeanWrapper beanWrapper = new BeanWrapperImpl(person);// 注册自定义 PropertyEditorbeanWrapper.registerCustomEditor(Date.class, new DateEditor());// 设置日期属性beanWrapper.setPropertyValue("birthdate", "2023-01-01");// 获取日期属性Date birthdate = (Date) beanWrapper.getPropertyValue("birthdate");System.out.println("Birthdate: " + birthdate); // 输出: Birthdate: Sun Jan 01 00:00:00 CST 2023}
}class Person {private String name;private int age;private Date birthdate;// Getters and Setterspublic 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 Date getBirthdate() { return birthdate; }public void setBirthdate(Date birthdate) { this.birthdate = birthdate; }
}class DateEditor extends PropertyEditorSupport {@Overridepublic void setAsText(String text) throws IllegalArgumentException {try {setValue(new SimpleDateFormat("yyyy-MM-dd").parse(text));} catch (ParseException e) {throw new IllegalArgumentException("Invalid date format");}}
}

 处理属性设置错误

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.PropertyAccessException;public class ErrorHandling {public static void main(String[] args) {Person person = new Person();BeanWrapper beanWrapper = new BeanWrapperImpl(person);try {// 尝试设置一个不存在的属性beanWrapper.setPropertyValue("invalidProperty", "value");} catch (PropertyAccessException e) {System.out.println("Error: " + e.getMessage());
// 输出: Error: Invalid property 'invalidProperty' of bean class [Person]: 
// Bean property 'invalidProperty' is not readable or has an invalid getter 
// method: Does the return type of the getter match the parameter type 
// of the setter?}}
}class Person {private String name;private int age;// Getters and Setterspublic 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; }
}

 

http://www.hrbkazy.com/news/44654.html

相关文章:

  • 各行各业网站建设深圳网站seo推广
  • 吴江区城乡建设管理局网站免费网站在线观看人数在哪
  • 如何做关于网站推广的培训seo搜索引擎优化岗位要求
  • 两学一做测试网站网络推广计划方案
  • 开发公众号 微网站开发免费发布信息网网站
  • 网站建设公司上海seo关键词排名价格
  • 小米新品发布会优化设计七年级上册语文答案
  • 网站动态背景怎么做百度网址名称是什么
  • 给企业做网站 内容需要对方提供成都百度推广公司联系电话
  • wap网站开发教程专业seo网络营销公司
  • 网站建设说课获奖视频品牌全网推广
  • 南通网站建设有限公司域名被墙查询检测
  • 班级展示网站百度一下官方入口
  • 怎样做网站平台赚钱软文宣传推广
  • 福田企业网站建设优化网站结构一般包括
  • 哪些做任务的网站靠谱seo策略是什么意思
  • 用wordpress建一个网站黄页网络的推广软件
  • 网站建设图片链接方法深圳全网推广服务
  • 如何做kindle电子书下载网站企业宣传
  • 杭州网站建设响应式中国新闻
  • 自己做网站的成本要哪些东西苏州网站优化公司
  • 实用电子商务网站建立推广关键词排名查询
  • 平台规划方案怎么写网站推广怎么优化
  • 早教网站源码网站建设方案书范文
  • 最新网站建设软件中国企业500强排行榜
  • 做公司的网站有哪些百度站内搜索提升关键词排名
  • 网站开发要用cms地推项目发布平台
  • wordpress写文章页面样式问题手机seo快速排名
  • wap手机网站描述正确的是软文标题大全
  • 电商网站英文怎样做网络推广效果好