1
- [[beans -binding]]
1
+ [[data -binding]]
2
2
= Data Binding
3
3
4
4
Data binding is useful for binding user input to a target object where user input is a map
5
- with property paths as keys, following xref:beans-beans -conventions[JavaBeans conventions].
5
+ with property paths as keys, following xref:data-binding -conventions[JavaBeans conventions].
6
6
`DataBinder` is the main class that supports this, and it provides two ways to bind user
7
7
input:
8
8
9
- - xref:beans- constructor-binding[Constructor binding] - bind user input to a public data
10
- constructor, looking up constructor argument values in the user input.
11
- - xref:beans-beans [Property binding] - bind user input to setters, matching keys from the
12
- user input to properties of the target object structure.
9
+ - xref:data-binding- constructor-binding[Constructor binding] - bind user input to a
10
+ public data constructor, looking up constructor argument values in the user input.
11
+ - xref:data-binding-property-binding [Property binding] - bind user input to setters,
12
+ matching keys from the user input to properties of the target object structure.
13
13
14
14
You can apply both constructor and property binding or only one.
15
15
16
16
17
- [[beans -constructor-binding]]
17
+ [[data-binding -constructor-binding]]
18
18
== Constructor Binding
19
19
20
20
To use constructor binding:
@@ -32,7 +32,7 @@ WebFlux support a custom name mapping through the `@BindParam` annotation on con
32
32
parameters or fields if present. If necessary, you can also configure a `NameResolver` on
33
33
`DataBinder` to customize the argument name to use.
34
34
35
- xref:beans-beans -conventions[Type conversion] is applied as needed to convert user input.
35
+ xref:data-binding -conventions[Type conversion] is applied as needed to convert user input.
36
36
If the constructor parameter is an object, it is constructed recursively in the same
37
37
manner, but through a nested property path. That means constructor binding creates both
38
38
the target object and any objects it contains.
@@ -46,9 +46,7 @@ If the target is created successfully, then `target` is set to the created insta
46
46
after the call to `construct`.
47
47
48
48
49
-
50
-
51
- [[beans-beans]]
49
+ [[data-binding-property-binding]]
52
50
== Property Binding with `BeanWrapper`
53
51
54
52
The `org.springframework.beans` package adheres to the JavaBeans standard.
@@ -74,15 +72,14 @@ The way the `BeanWrapper` works is partly indicated by its name: it wraps a bean
74
72
perform actions on that bean, such as setting and retrieving properties.
75
73
76
74
77
-
78
- [[beans-beans-conventions]]
75
+ [[data-binding-conventions]]
79
76
=== Setting and Getting Basic and Nested Properties
80
77
81
78
Setting and getting properties is done through the `setPropertyValue` and
82
79
`getPropertyValue` overloaded method variants of `BeanWrapper`. See their Javadoc for
83
80
details. The below table shows some examples of these conventions:
84
81
85
- [[beans-beans -conventions-properties-tbl]]
82
+ [[data-binding -conventions-properties-tbl]]
86
83
.Examples of properties
87
84
|===
88
85
| Expression| Explanation
@@ -106,7 +103,7 @@ details. The below table shows some examples of these conventions:
106
103
(This next section is not vitally important to you if you do not plan to work with
107
104
the `BeanWrapper` directly. If you use only the `DataBinder` and the `BeanFactory`
108
105
and their default implementations, you should skip ahead to the
109
- xref:core/validation/beans-beans .adoc#beans-beans -conversion[section on `PropertyEditors`].)
106
+ xref:core/validation/data-binding .adoc#data-binding -conversion[section on `PropertyEditors`].)
110
107
111
108
The following two example classes use the `BeanWrapper` to get and set
112
109
properties:
@@ -239,9 +236,8 @@ Kotlin::
239
236
======
240
237
241
238
242
-
243
- [[beans-beans-conversion]]
244
- == ``PropertyEditor``'s
239
+ [[data-binding-conversion]]
240
+ == ``PropertyEditor``s
245
241
246
242
Spring uses the concept of a `PropertyEditor` to effect the conversion between an
247
243
`Object` and a `String`. It can be handy
@@ -272,7 +268,7 @@ package. Most, (but not all, as indicated in the following table) are, by defaul
272
268
still register your own variant to override the default one. The following table describes
273
269
the various `PropertyEditor` implementations that Spring provides:
274
270
275
- [[beans-beans -property-editors-tbl]]
271
+ [[data-binding -property-editors-tbl]]
276
272
.Built-in `PropertyEditor` Implementations
277
273
[cols="30%,70%"]
278
274
|===
@@ -426,8 +422,8 @@ Kotlin::
426
422
======
427
423
428
424
429
- [[beans-beans -conversion-customeditor-registration]]
430
- === Custom ``PropertyEditor``' s
425
+ [[data-binding -conversion-customeditor-registration]]
426
+ === Custom ``PropertyEditor``s
431
427
432
428
When setting bean properties as string values, a Spring IoC container ultimately uses
433
429
standard JavaBeans `PropertyEditor` implementations to convert these strings to the complex type of the
@@ -451,7 +447,7 @@ where it can be automatically detected and applied.
451
447
Note that all bean factories and application contexts automatically use a number of
452
448
built-in property editors, through their use of a `BeanWrapper` to
453
449
handle property conversions. The standard property editors that the `BeanWrapper`
454
- registers are listed in the xref:core/validation/beans-beans .adoc#beans-beans -conversion[previous section].
450
+ registers are listed in the xref:core/validation/data-binding .adoc#data-binding -conversion[previous section].
455
451
Additionally, ``ApplicationContext``s also override or add additional editors to handle
456
452
resource lookups in a manner appropriate to the specific application context type.
457
453
@@ -569,7 +565,7 @@ Finally, the following example shows how to use `CustomEditorConfigurer` to regi
569
565
</bean>
570
566
----
571
567
572
- [[beans-beans -conversion-customeditor-registration-per]]
568
+ [[data-binding -conversion-customeditor-registration-per]]
573
569
=== `PropertyEditorRegistrar`
574
570
575
571
Another mechanism for registering property editors with the Spring container is to
@@ -580,7 +576,7 @@ You can write a corresponding registrar and reuse it in each case.
580
576
`PropertyEditorRegistry`, an interface that is implemented by the Spring `BeanWrapper`
581
577
(and `DataBinder`). `PropertyEditorRegistrar` instances are particularly convenient
582
578
when used in conjunction with `CustomEditorConfigurer` (described
583
- xref:core/validation/beans-beans .adoc#beans-beans -conversion-customeditor-registration[here]), which exposes a property
579
+ xref:core/validation/data-binding .adoc#data-binding -conversion-customeditor-registration[here]), which exposes a property
584
580
called `setPropertyEditorRegistrars(..)`. `PropertyEditorRegistrar` instances added
585
581
to a `CustomEditorConfigurer` in this fashion can easily be shared with `DataBinder` and
586
582
Spring MVC controllers. Furthermore, it avoids the need for synchronization on custom
@@ -703,7 +699,3 @@ This style of `PropertyEditor` registration can lead to concise code (the implem
703
699
of the `@InitBinder` method is only one line long) and lets common `PropertyEditor`
704
700
registration code be encapsulated in a class and then shared amongst as many controllers
705
701
as needed.
706
-
707
-
708
-
709
-
0 commit comments