• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Uses of SpEL

 
Greenhorn
Posts: 29
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Craig.

Can you provide a brief example or two for the new Spring Expression Language? Is this at all similar to OGNL?
 
author
Posts: 422
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure...The simplest examples of SpEL are things that wire things like system properties or environment variables into Spring bean properties. For example, you could wire a COUNTRY_CODE environment variable into a property like this:



Or, if you prefer annotation-oriented wiring, you could do the same thing with @Value:



That's a useful way of using SpEL, but you can do other cool stuff, too. Like, let's say that instead of wiring in the COUNTRY_CODE, you use it to decide among two other beans to wire in...For example:



In this case, if COUNTY_CODE is "CDN", then it will wire in a bean whose ID is "canadianTaxProcessor". Otherwise, it will wire in the bean whose ID is "usTaxProcessor".

Spring Security 3.0 also uses SpEL. Imagine a bank application where tellers can perform withdrawals up to $10,000. But it requires supervisor authority to withdraw greater amounts. In that case, the withdraw() method might be annotated like this:


 
Rob Ivan
Greenhorn
Posts: 29
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the examples. That is exactly what I was looking for.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So there's a bit of COP (tm :( thrown in, similar to SpringContracts, but with a richer language? In the above example where would hasRole() live/be implemented/specified?
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:... where would hasRole() live/be implemented/specified?



That feature intrigued me when I saw it in the SpEL examples. It appears that hasRole() is part of the Spring Security 3.0 API supported by a Spring xml configuration file that maps all the roles to the entries in a back-end source such as an LDAP server. That's all I learned so far in some Spring Security tutorials.
 
Craig Walls
author
Posts: 422
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly. SpEL has a few out-of-the-box features with Spring 3, but can be extended. Spring Security 3 extends it with hasRole()...among other things.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, potentially very cool. So we can extend SpEL with our own functions, handlers, whatever? Are those "plugins" also Spring-configured? (And what are they called?)
 
Craig Walls
author
Posts: 422
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SpEL can be extended programmatically for sure. But unless I've overlooked something, there's no way to declaratively add new functions to SpEL (and yes, I have looked...I thought it would be very useful to do that).

Spring Security doesn't extend the exact same instance of SpEL that's used for bean wiring...it just creates a new SpEL parser and adds its custom stuff. You could always do the same thing for whatever SpEL purposes you have in mind.

But if you want to extend the bean-wiring uses of SpEL, I know of no way to do that (but would love it if someone would prove me wrong and show me how).
 
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, that means using SpEL, we can inject dynamic values into Annotations. And these values can be environment specific, which is cool.

If I understand SpEL can be used in,

1) Annotations
2) XML bean definitions


anywhere else?

Thanks.
 
Craig Walls
author
Posts: 422
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use SpEL programmatically as a general purpose expression language (much like you might use OGNL). And you can use it anywhere else that has added SpEL support, such as Spring Security 3. Supposedly, Spring Web Flow 3 will also offer SpEL as an alternate EL along with OGNL and Unified EL (both of which it already supports).
 
Anil Vupputuri
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Craig Walls wrote:You can also use SpEL programmatically as a general purpose expression language (much like you might use OGNL). And you can use it anywhere else that has added SpEL support, such as Spring Security 3. Supposedly, Spring Web Flow 3 will also offer SpEL as an alternate EL along with OGNL and Unified EL (both of which it already supports).



Cool. Embedding language (SpEL) within language (Java), can we expect SpEL (as opposed to JSTL) to be used in JSP in future?
 
Craig Walls
author
Posts: 422
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, SpEL and JSTL are two different things. But if you mean SpEL vs. JSP EL, then...I *think* that the newest version of Spring (3.0.1) includes the ability to use SpEL expressions as values in Spring's JSP tags. I am not sure...I know that it was being worked on, but I haven't had a chance to see if it made it into the release or not.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSTL and SpEL are completely different things. And SpEL isn't really "embedded" in the Java language, any more than OGNL or MVEL are.

Although now I'm thinking about plugging SpEL in to the Struts 2 tags.
 
Anil Vupputuri
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:I'm thinking about plugging SpEL in to the Struts 2 tags.



You mean you can include SpEL using JSP tag in Struts2.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you're asking, but the EL in the S2 tags in theoretically pluggable; I know there was an effort to use both JSP EL and MVEL as options.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic