Saturday, April 21, 2012

Spring Expression Langauge and Simple Bean References

In this post I'll demonstrate a simple use of Spring Expression Language (SpEL). First, I'll give a little background of the "problem" I used to solve, but feel free to skip ahead to the Spring configuration below.

The project in question was required to make two separate RESTful calls to the same service, for example:


The URLs contain the same base path,, and two separate paths, record and order. The URLs are to be looked up in a service directory, whose entries we have control of.

At first I considered placing both URLs in the directory to avoid having to deal with adding each path to the base URL in code. This sounded like a misuse of the directory however as each URL would not represent a separate service deployment. Then I remembered reading about SpEL and thought this could be help me add the paths to the URL in good ol' Spring configuration.

Here is a simplification of the defintion of the bean for the URL ( we will reference for defining the two service URLs:

    <bean id="serviceUrl" class="org.mycompany.service.entry.ServiceEntryProxy">
       <property name="serviceName" value="businessService"/>
       <property name="serviceDirectory" ref="serviceDirectory"/>

As we planned on using Spring's RestTemplate to invoke the remote service, we can convert the returned by the service lookup and translate them to Strings to be injected in the calling class.

    <bean id="recordServiceUrl" class="java.lang.String">
        <constructor-arg value="#{serviceUrl.toExternalForm()}/record"/>

    <bean id="orderServiceUrl" class="java.lang.String">
        <constructor-arg value="#{serviceUrl.toExternalForm()}/order"/>

We create the expressions above using #{...}, which is similar to syntax used to resolve properies in Spring, ${...}. Inside the expression, we can reference the serviceUrl bean defined earlier and call a method on it, #{serviceUrl.toExternalForm()}, which returns a String representation of the URL. From there we can add the remaining paths, /record and /order.

Given the following property signature of the class required to invoke the service:

    public class ServiceController {
        private RestTemplate restTemplate;

        private String recordUrl;
        private String orderUrl;

        // ...

We can inject the URL string defined earlier:

    <bean id="serviceController" class="">
        <property name="restTemplate" ref="restTemplate"/>
        <property name="recordUrl" ref="recordUrl"/>
        <property name="orderUrl" ref="orderUrl"/>            

    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>

Admittedly, this is a very simple example of what we can do with SpEL. The feature overview in the documentation does a much better job of enumerating them than I could try to do. Hopefully, now that I've used in once, I'll look to SpEL again for other uses to help get out of some potentially tricky situations.


  1. You have posted awesome news, keep it up. Thanks for sharing.

  2. Hi,

    Myself Heena Kapoor from patparganj.....i would like to share my experience.
    i was just going through this blog ...where i found a site
    i must say it is a great step in real estate sector which helped me immensely and guys are doing good as they have strong data regarding local property dealers in east delhi.
    i just called on 011-6555-6555 and it saved my time in finding local dealers in east delhi.
    Thanks arjun , keep it up.

  3. Are you looking for Office Space and Property in east delhi? 3 BHK good society flat complete woodwork all facility available prime location near market and park more information more option on contact.

  4. Property Dealers in Pitampura-
    Call 011-6555-6555 and increase the number of options for the property you are looking for.
    Try out the best real estate options by going on or by calling 011-6555-6555.

  5. Property mesh Provides Property in delhi, Residential Property in delhi, Delhi Property dealers, Commercial Property in delhi, Flats in delhi, Delhi flats

  6. This comment has been removed by the author.

  7. This comment has been removed by the author.

  8. Residential Property in Yamuna Expressway
    :-very nice information, we are glad to read this news or post,

  9. Thanks for sharing.!!!!!!.
    You have posted awesome news, keep it up.
    Thanks for sharing again
    Property Dealers In East Delhi

  10. I think, this is really very well information about parking information. thanks for providing this information.
    Flats for Sale in Mayur Vihar Ph-I New Delhi

  11. If you are looking for affordable home in Panchseel Envlave New Delhi location than you are at right place. here you have option to choose 1BHK and 2BHK lavish home in various sizes.Flats for Sale in Panchsheel Enclave New Delhi

  12. I think, this is really very well written about Spring Expression Langauge and Simple Bean References. Thanks for giving this information in front of me.
    2 Bhk In Dwarka

  13. Search Real Estate in Faridabad –buy ,sell , rent, Commercial Property in delhi Gurgaon by real Property owners, dealers, builders and real estate agents . Find your dream home here .

  14. Search Real Estate in Faridabad –buy ,sell , rent, Commercial Property in delhi Gurgaon by real Property owners, dealers, builders and real estate agents . Find your dream home here .

  15. Thanks for taking the time to discuss this, would you mind updating your blog with more information? It is extremely helpful for me.

    Boca Isles Real Estate & Woodfield Country Club Real Estate

  16. This is so impressive and nice title of blog. I am always searching such type of Blog that gave some useful knowledge and relax our mind. So Very thanks for design this type of content and what are you thing at time of write. Thanks for sharing views.

    Buy Townhouses in Mississauga & Detached Homes in Mississauga

  17. Well information given about Spring Expression Langauge and Simple Bean References. Thanks for given this information here about this blog.
    Plot Sale In Chennai

  18. Commercial office space available in patparganj industrial area fully furnished for rent area measuring 600 sq. ft. .Don’t miss it and grab the best deals of east delhi in patparganj indsutrial area. For sale - purchase - rent of any kind of property in patparganj indsutrial area if you are looking for then just logon to and make your search easy.

    Commercial Space for rent in Kaushambi
    Office space for rent in laxmi nagar

    Office space For rent in karkardooma

    Office space for rent in Preet Vihar

    Office space For rent in kaushambi

    Commercial Real estate website Delhi /NCR -

  19. Office space for rent inpatparganj industrial is the only site in India where you can find the best properties in patparganj indsutrial area saving you time and making the task fast.

    Office space for rent in Laxmi Nagar

    Office space for rent in patparganj Industrial area

  20. you are searching for best bridal mehndi artist in delhi,Bridal Mehndi Designer in Delhi, Top Bridal Mehandi Artists in Delhi

    Bridal Mehndi Designer And Artist in Delhi

  21. Finding a Office space for rent in laxmi nagar, delhi is not an easy job; it is a full time investment. Especially, for the people who don’t have any information regarding properties in Delhi NCR. Every person desires their own property in Metro Cities. But due to lack of information about properties people get little bit confused. If you are also in the same confusion and want to buy the residential Properties in Delhi NCR then Relax there is something you must know

    Office space for rent in Laxmi Nagar

    Property Dealers in Allahabad, Uttar Pradesh