Spring spel expression usage example
Spring in action third edition reading notes
spring3. 0 introduces spring expression language (spel), which we can implement through spel
1. Reference the bean by its ID
2. Calling methods and properties in reference objects
3. Calculate the value of the expression
4. Regular expression matching
5. Collection operation
The ultimate goal of spel is to get the calculated value of the expression. These expressions may be some enumerated values, some attributes of the reference object, or some constants in the class. Complex spel expressions are usually composed of some simple elements. The simplest thing is to get the values of some given elements, for example:
References to other beans
Through spel, we can also reference other beans in the context
Equivalent to
Reference another bean with ID Saxophone as the value of the instrument to reference a property in other beans
Take the song field of the bean with ID Kenny as the reference of the song value to a method in other beans
Call the selectsong () method of the bean with ID songselector and use its return value as the value of song, which also brings a problem. If the selectsong () method returns null, a null pointer exception will be thrown
< property name = "song" value = "#{songselector. Selectsong()?. touppercase()}" / >, expression (?.) You can ensure that the touppercase () method is called when the return of selectsong () is not empty. If the return is empty, the subsequent methods will not be called
Reference to class
If a class is an external class rather than a bean defined in spring, how do you reference it?
Use the expression t (), for example:
Spel evaluates an expression
Spel expressions support a variety of operators. We can use these operators to calculate the value of the expression
Filter elements from a collection using spel:
Define a collection using the element < util: list > in spring's util namespace
Filter collections using spel
[] operator can also filter the map, assuming that citis is a map type < property name = "chosencity" value = "#{cities [" keyname "]}" / > [] operate on the properties type
< util: properties id = "Settings" location = "classpath: settings. Properties" / > use the < util: Properties > tag to read a properties file
summary
The above is all about the spring spel expression usage example in this article. I hope it will be helpful to you.