Spring Data Conversion

Source: Internet
Author: User

Spring3 introduces a more general type conversion system, which defines the SPI interface (converter, etc.) and the corresponding runtime execution type conversion API (Conversionservice, etc.), which in spring is similar to the PropertyEditor function, You can substitute PropertyEditor to convert the value of an external bean property to the type required by the Bean property.

The type conversion system is spring-generic and is defined in the Org.springframework.core.convert package, not just in the spring Web MVC scenario. The goal is to completely replace the propertyeditor, providing a stateless, strongly typed type conversion system that can be converted between any type, and can be used wherever needed, such as spel, data binding.

1. Type converter: provides implementation support for type conversions.

2. Type converter registrar, type Conversion Service: provides type converter registration support, runtime type conversion API support.

Defaultconversionservice: The default type conversion service implementation;

Defaultformattingconversionservice: A type conversion service implementation with data formatting support that is typically implemented with this service.

3. The spring built-in type converter is as follows:

Class name

Description

First group: Scalar converters

Stringtobooleanconverter

String----->boolean

TRUE:TRUE/ON/YES/1; false:false/off/no/0

Objecttostringconverter

Object----->string

Call the ToString Method transformation

Stringtonumberconverterfactory

String----->number (such as Integer, Long, and so on)

Numbertonumberconverterfactory

Number subtype (integer, long, double, etc.) <--> number subtype (integer, long, double, etc.)

Stringtocharacterconverter

String----->java.lang.character

Takes the first character of a string

Numbertocharacterconverter

Number subtype (Integer, Long, double, and so on)-Java.lang.Character

Charactertonumberfactory

Java.lang.character-->number subtype (Integer, Long, double, etc.)

Stringtoenumconverterfactory

String----->enum Type

Convert a string to the required enum type by enum.valueof

Enumtostringconverter

Enum type----->string

Returns the name () value of an Enum object

Stringtolocaleconverter

String----->java.util.local

Propertiestostringconverter

Java.util.Properties----->string

Default decoding via Iso-8859-1

Stringtopropertiesconverter

String----->java.util.properties

Use ISO-8859-1 encoding by default

Second group: sets, array-dependent converters

Arraytocollectionconverter

Arbitrary s array----> arbitrary t Set (List, set)

Collectiontoarrayconverter

Any t set (List, set)----> arbitrary s array

Arraytoarrayconverter

Arbitrary s Array <----> arbitrary t array

Collectiontocollectionconverter

Any t set (list, set) <----> Any T collection (list, set)

That is, type conversions between collections

Maptomapconverter

Conversion between map<---->map

Arraytostringconverter

Arbitrary s array---->string type

Stringtoarrayconverter

String-----> Array

The default is split by "," and the whitespace (trim) is removed from both sides of the string.

Arraytoobjectconverter

Arbitrary s array----> conversion of any object

(returns the source object directly if the target type is compatible with the source type; otherwise returns the first element of the S array and casts the type)

Objecttoarrayconverter

Object-----> Unit Prime Group

Collectiontostringconverter

Any t set (List, set)---->string type

Stringtocollectionconverter

String-----> collection (List, set)

The default is split by "," and the whitespace (trim) is removed from both sides of the string.

Collectiontoobjectconverter

Any t set----> Conversion of any object

(returns the source object directly if the target type is compatible with the source type; otherwise returns the first element of the S array and casts the type)

Objecttocollectionconverter

Object-----> Single Element collection

Third group: Default (Fallback) Converter: The previous converter cannot be converted when called

Objecttoobjectconverter

Object (S)----->object (T)

First try valueof for conversion, no then try new constructor (S)

Idtoentityconverter

Id (S)----->entity (T)

Find and call public static T Find[entityname] (S) Gets the target object, EntityName is a simple type of type T

Fallbackobjecttostringconverter

Object----->string

Conversionservice called (The ToString () method of the executing object) as a recovery use, that is, when other converters cannot be converted

S: Represents the source Type, T: Represents the target type

The converters above are automatically registered when using the transform service to implement Defaultconversionservice and Defaultformattingconversionservice.

Spring Data Conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.