geeky · java

SPEL! I conquered you on a Friday!

Today is my first attempt at using SPEL. After a mighty struggle, I was able to conquer it!

Background:

I needed a year variable for some work on a jsp page. It’s not always defined. I know for our routable datasource, a default year is required in order for it to work. I wanted to use the same default year but I only want the variable to be defined once and used in both places.

So I looked into SPEL.

It seems like the correct solution for the problem.

My original idea:


As it turns out, SPEL does not yet support embedded variables. SPEL, you must improve yourself!

My workaround:

I do not love it but it accomplishes my goal of not defining it more than one place.

Define the variable in a properties file e.g. application.properties
defaultYear=2011

Then use placeholder instead of SPEL notation to include it in spring.

Syntax grrr syntax:

Sadly some of my time was to learn the fact you CANNOT put a space between # and { in SPEL. For example, # {bean.property} will NOT work, but #{bean.property} will. It seems obvious afterwards but the error message you get just DOES NOT help you come to that conclusion.

Moral of the story:

SPEL is very handy but it’s yet perfect. Do keep that in mind as an alternative solution when dealing with Spring. If you use annotation, you can do @Value(“#{bean.property}”). Refer documentation for more info.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s