- 浏览: 90204 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
hvang1988:
...
Spring --- Transaction Management -
Branding:
谢谢,原来总是记不住,这下知道原理了
“Hello Java” -- Java安装
一) BeanWrapper
BeanWrapper这个类一般不会被Spring的使用者直接调用,而是使用DataBinder和BeanFactory这两个类是间接被调用的。但是知道BeanWrapper的使用方式对于理解Spring的数据绑定机制还是十分用益的。下面我们就来看个BeanWrapper被直接调用的例子:
这便是BeanWrapper的核心作用——数据绑定!
二)PropertyEditor -- 属性编辑器
Spring的数据绑定非常强大。属性编辑器PropertyEditor的主要功能就是将外部的设置值转换为JVM内部的对应类型,所以属性编辑器其实就是一个类型转换器。它负责String与Object之间的转换。Spring本身已经定义了许多种类的属性编辑器,所以一些常用类型的转换已经不需要我们再关心了。我们关心的应该是如何自定义的PropertyEditor去满足某些特殊类的转换。
你可以有两种方式定义PropertyEditor,但不管采用哪种Spring都使用java.beans.PropertyEditorManager类来搜寻一个类所对应的属性编辑器。
1' 第一种当然是默认路径。
如图,FooEditor便是Foo的属性编辑器了。在同一Package下,Spring默认类名+"Editor" 便是该类的属性编辑器。
2'当然,第二种你也可以自定义属性编辑器的位置和名称。
注:如果使用BeanFactory,用户需要手工调用registerCustomEditor(Class requiredType, PropertyEditor propertyEditor)方法注册自定义属性编辑器;如果使用ApplicationContext,则只需要在配置文件通过CustomEditorConfigurer注册就可以了。一般情况下,我们当然使用ApplicationContext。如上面这个例子,我们自定义了一个名叫ExoticTypeEditor的属性编辑器,它专门负责对ExoticType类的转换。具体的代码如下:
注:一般地,我们要使用PropertyEditor时,并不直接实现此接口,而是通过继承实现此接口的java.beans.PropertyEditorSupport来简化我们的工作,在子类覆盖setAsText方法就可以了,setValue方法一般不直接使用,在setAsText方法中将字符串进行转换并产生目标对象以后,由调setAsText调用setValue来把目标对象注入到编辑器中。当然,你可用覆盖更多的方法来满足你的特殊要求。
这样当我们遇到如下情况时,我们的自定义属性编辑器便会被触发使用(反射机制):
3' 刚说了只有两种定义属性编辑器的方式,这会儿咋又来第三种呢?呵呵,这种方式确实有点..."另类"?但它的作用非常大,它可以满足一个类中用到多个属性编辑器的情况:
还是首先讲讲命名规则,如图,FooBeanInfo便是Foo类的BeanInfo了。同一Package下,类名+"BeanInfo" 便是该类对应的属性编辑器,与之前不同的是,它说明了该了中需要使用到的一个或多个子属性编辑器。而它的代码实现如下:
注:BeanInfo接口有一个常用的实现类:SimpleBeanInfo,一般情况下,可以通过扩展SimpleBeanInfo实现自己的功能。
三)PropertyEditorRegistrars
这个数据绑定接口结合Spring MVC模块一起使用效果非常好。结合一个例子就非常容易明白:
首先是声明和配置:
而后是在Spring MVC中使用它:
这样就实现了http请求参数与对应类的转换。而且代码还可高度重用,非常好的一个设计!
BeanWrapper这个类一般不会被Spring的使用者直接调用,而是使用DataBinder和BeanFactory这两个类是间接被调用的。但是知道BeanWrapper的使用方式对于理解Spring的数据绑定机制还是十分用益的。下面我们就来看个BeanWrapper被直接调用的例子:
//首先是两个对象类Company Employee public class Company { private String name; private Employee managingDirector; public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Employee getManagingDirector() { return this.managingDirector; } public void setManagingDirector(Employee managingDirector) { this.managingDirector = managingDirector; } } public class Employee { private String name; private float salary; public String getName() { return this.name; } public void setName(String name) { this.name = name; } public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } } BeanWrapper company = BeanWrapperImpl(new Company()); // setting the company name.. company.setPropertyValue("name", "Some Company Inc."); // ... can also be done like this: PropertyValue value = new PropertyValue("name", "Some Company Inc."); company.setPropertyValue(value); // ok, let's create the director and tie it to the company: BeanWrapper jim = BeanWrapperImpl(new Employee()); jim.setPropertyValue("name", "Jim Stravinsky"); company.setPropertyValue("managingDirector", jim.getWrappedInstance()); // retrieving the salary of the managingDirector through the company Float salary = (Float) company.getPropertyValue("managingDirector.salary");
这便是BeanWrapper的核心作用——数据绑定!
二)PropertyEditor -- 属性编辑器
Spring的数据绑定非常强大。属性编辑器PropertyEditor的主要功能就是将外部的设置值转换为JVM内部的对应类型,所以属性编辑器其实就是一个类型转换器。它负责String与Object之间的转换。Spring本身已经定义了许多种类的属性编辑器,所以一些常用类型的转换已经不需要我们再关心了。我们关心的应该是如何自定义的PropertyEditor去满足某些特殊类的转换。
你可以有两种方式定义PropertyEditor,但不管采用哪种Spring都使用java.beans.PropertyEditorManager类来搜寻一个类所对应的属性编辑器。
1' 第一种当然是默认路径。
如图,FooEditor便是Foo的属性编辑器了。在同一Package下,Spring默认类名+"Editor" 便是该类的属性编辑器。
2'当然,第二种你也可以自定义属性编辑器的位置和名称。
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="example.ExoticType" value="example.ExoticTypeEditor"/> </map> </property> </bean>
注:如果使用BeanFactory,用户需要手工调用registerCustomEditor(Class requiredType, PropertyEditor propertyEditor)方法注册自定义属性编辑器;如果使用ApplicationContext,则只需要在配置文件通过CustomEditorConfigurer注册就可以了。一般情况下,我们当然使用ApplicationContext。如上面这个例子,我们自定义了一个名叫ExoticTypeEditor的属性编辑器,它专门负责对ExoticType类的转换。具体的代码如下:
// converts string representation to ExoticType object package example; public class ExoticTypeEditor extends PropertyEditorSupport { public void setAsText(String text) { setValue(new ExoticType(text.toUpperCase())); } } --------------------------------------------------------------- package example; public class ExoticType { private String name; public ExoticType(String name) { this.name = name; } }
注:一般地,我们要使用PropertyEditor时,并不直接实现此接口,而是通过继承实现此接口的java.beans.PropertyEditorSupport来简化我们的工作,在子类覆盖setAsText方法就可以了,setValue方法一般不直接使用,在setAsText方法中将字符串进行转换并产生目标对象以后,由调setAsText调用setValue来把目标对象注入到编辑器中。当然,你可用覆盖更多的方法来满足你的特殊要求。
这样当我们遇到如下情况时,我们的自定义属性编辑器便会被触发使用(反射机制):
public class DependsOnExoticType { private ExoticType type; public void setType(ExoticType type) { this.type = type; } } <bean id="sample" class="example.DependsOnExoticType"> <property name="type" value="aNameForExoticType"/> </bean>
3' 刚说了只有两种定义属性编辑器的方式,这会儿咋又来第三种呢?呵呵,这种方式确实有点..."另类"?但它的作用非常大,它可以满足一个类中用到多个属性编辑器的情况:
还是首先讲讲命名规则,如图,FooBeanInfo便是Foo类的BeanInfo了。同一Package下,类名+"BeanInfo" 便是该类对应的属性编辑器,与之前不同的是,它说明了该了中需要使用到的一个或多个子属性编辑器。而它的代码实现如下:
public class FooBeanInfo extends SimpleBeanInfo { public PropertyDescriptor[] getPropertyDescriptors() { try { final PropertyEditor numberPE = new CustomNumberEditor(Integer.class, true); PropertyDescriptor ageDescriptor = new PropertyDescriptor("age", Foo.class) { public PropertyEditor createPropertyEditor(Object bean) { return numberPE; }; }; return new PropertyDescriptor[] { ageDescriptor }; } catch (IntrospectionException ex) { throw new Error(ex.toString()); } } }
注:BeanInfo接口有一个常用的实现类:SimpleBeanInfo,一般情况下,可以通过扩展SimpleBeanInfo实现自己的功能。
三)PropertyEditorRegistrars
这个数据绑定接口结合Spring MVC模块一起使用效果非常好。结合一个例子就非常容易明白:
首先是声明和配置:
package com.foo.editors.spring; public final class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar { public void registerCustomEditors(PropertyEditorRegistry registry) { // it is expected that new PropertyEditor instances are created registry.registerCustomEditor(ExoticType.class, new ExoticTypeEditor()); // you could register as many custom property editors as are required here... } } <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="propertyEditorRegistrars"> <list> <ref bean="customPropertyEditorRegistrar"/> </list> </property> </bean> <bean id="customPropertyEditorRegistrar" class="com.foo.editors.spring.CustomPropertyEditorRegistrar"/>
而后是在Spring MVC中使用它:
public final class RegisterUserController extends SimpleFormController { private final PropertyEditorRegistrar customPropertyEditorRegistrar; public RegisterUserController(PropertyEditorRegistrar propertyEditorRegistrar) { this.customPropertyEditorRegistrar = propertyEditorRegistrar; } protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { this.customPropertyEditorRegistrar.registerCustomEditors(binder); } // other methods to do with registering a User }
这样就实现了http请求参数与对应类的转换。而且代码还可高度重用,非常好的一个设计!
发表评论
-
Spring --- Transaction Management
2012-12-26 09:52 10634一)spring的事务管理 事务管理并非spring独 ... -
Spring --- AOP IV
2012-12-21 16:44 954Spring AOP APIs 一)一个最简单的例子 publ ... -
Spring --- AOP III
2012-12-19 13:19 1036这篇文章先补充两个AOP的概念: 一)引入(Introduct ... -
Spring --- AOP II
2012-12-17 19:29 1152一)Spring AOP---schema-based ... -
Spring --- AOP
2012-12-13 16:10 1148一)什么是AOP AOP(A ... -
Spring --- SpEL
2012-12-11 16:10 9552一)什么是SpEL SpEL -- Spring Expr ... -
Spring --- Validation
2012-12-07 15:05 1802一) Validator接口 Spring的Validat ... -
Spring --- Resource
2012-12-06 16:50 1553一)如何使用spring中的r ... -
Spring --- IOC III
2012-12-04 15:34 1287承接上两篇IOC的介绍,我们继续... 9)Applicati ... -
Spring --- IOC II
2012-11-30 15:43 1541承接前一篇Spring --- IOC,继续IOC的介绍 6 ... -
Spring --- IOC
2012-11-28 16:36 1112一) IOC其实很简单 什么是IOC(控制反转)? 这名 ... -
开篇简介--Spring is not only Spring Framework
2012-03-06 16:42 1726一)什么是Spring 在java领域,当我们提起Spri ...
相关推荐
29.3. JPA and “Spring Data” 29.3.1. Entity Classes 29.3.2. Spring Data JPA Repositories 29.3.3. Creating and Dropping JPA Databases 29.3.4. Open EntityManager in View 29.4. Using H2’s Web Console ...
工程文件 博文链接:https://starscream.iteye.com/blog/1072179
Spring-Formularios_Thymeleaf-_y-_Data_Binding
Chapter 11 – Validation and data binding in Spring Web MVC Chapter 12 –Developing RESTful web services using Spring Web MVC Chapter 13 – More Spring Web MVC – internationalization, file upload and...
Apache Camel provides support for Bean Binding and seamless integration with popular frameworks such as CDI, Spring, Blueprint and Guice. Camel also has extensive support for unit testing your routes.
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
Spring-Rest-Jackson-Json-数据绑定 代表性状态转移是一种软件体系结构样式,它定义了一组用于创建Web服务的约束。 符合REST架构风格的Web服务(称为Rest API)可在Internet上的计算机系统之间提供互操作性。我们在...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
Core technologies: dependency injection, events, resources, i18n, validation, data binding, type conversion, SpEL, AOP. Testing: mock objects, TestContext framework, Spring MVC Test, WebTestClient. ...
AngularJS is what would have been, had it been designed for building web-apps. Declarative templates with data-binding, MVC, dependency injection.
将Spring Data仓库暴露为REST端点 vii. 68. 数据库初始化 i. 68.1. 使用JPA初始化数据库 ii. 68.2. 使用Hibernate初始化数据库 iii. 68.3. 使用Spring JDBC初始化数据库 iv. 68.4. 初始化Spring Batch数据库 v. ...
CVE-2022-22963 复现Demo,A Spring MVC or Spring WebFlux application running on JDK 9+ may be vulnerable to remote code execution (RCE) via data binding. The specific exploit requires the application ...
The Spring Framework Chapter 2: Model 2 and the MVC Pattern Chapter 3: Introduction to Spring MVC Chapter 4: Annotation-Based Controllers Chapter 5: Data Binding and the Form Tag Library Chapter 6: ...
Mastering Spring Cloud Packt Upsell Why subscribe? PacktPub.com Contributors About the author About the reviewer Packt is searching for authors like you Preface Who this book is for What this book ...
Binding) 类型装换(Type Conversion) Spring 表达式(Spring Express Language) 面向切面编程(AOP) 数据存储(Data Access) JDBC 事务抽象(Transactions) DAO 支持(DAO Support) O/R映射(O/R Mapping) XML 编列(XML ...
Spring MVC(Model-View-Controller)是一个基于Java的MVC...7. 数据绑定(Data Binding) 8. 校验器(Validator) 这些资源描述了Spring MVC框架中不同组件的职责和作用,协同工作以构建可靠和可扩展的Web应用程序。
Model data and the data access layer to build high-performance, data-driven applications Work with reactive and functional programming strategies Use distribution techniques that support continuous ...
The first part of the book offers an in-depth introduction to jQuery fundamentals, which includes selecting elements, manipulating the DOM, and binding and reacting to browser books. After providing ...
搭建项目时使用了mybatisplus,项目能够正常启动,但在调用mapper方法查询数据库时报Invalid bound statement (not found)错误。本文给大家分享解决方案,感兴趣的朋友跟随小编一起看看吧
团结焊 的Unity数据绑定系统。 Unity-Weld是Unity 5+的库,可在Unity UI小部件和游戏/业务逻辑代码之间进行双向数据绑定。 这减少了样板代码,否则这些样板代码对于属性更改时更新UI来说是必需的,从而消除了场景中...