Thursday, January 16, 2014

Spring Course: V33 - V35 Spring Expression Language(SPEL)

V33 Introducing SPEL
spring SPEL allows you to write expresssions of this style #{...}

<property name="..." value="#{5+6}"/>

or you can call a method
<bean id="A" ..../>
<property name="..." value="#{A.getText()}"/>

there another thing which is called safe expression, lets say you wanna write
#{A.getText().toString()}
however getText() may return null, which will lead to null pointer expression, to write a safe expression you can write
#{A.getText()?.toString()}
so the question mark after getText() means that if the return value from getText() is null dont call what is after the question mark.

V34 using SPEL with Annotation
so we saw in the previous video how to use SPEL in beans.xml, you can also use it with annotation,

@Autowired
public void setProperty(@Value (#{....}) int x)

V35 Some useful SPEL operator

there are different SPEL syntax, check online for that, one example is to initialize a class like this

#{new java.text.DateFormat()}


No comments:

Post a Comment