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:

  1. http://service.mycompany.com/business/record
  2. http://service.mycompany.com/business/order

The URLs contain the same base path, http://service.mycompany.com/business, 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 (java.net.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"/>
    </bean>

As we planned on using Spring's RestTemplate to invoke the remote service, we can convert the java.net.URL 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>

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

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="org.mycompany.service.business.ServiceController">
        <property name="restTemplate" ref="restTemplate"/>
        <property name="recordUrl" ref="recordUrl"/>
        <property name="orderUrl" ref="orderUrl"/>            
    </bean>

    <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.

18 comments:

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

    ReplyDelete
  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
    http://www.propertydealerontips.com/
    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.

    ReplyDelete
  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.

    ReplyDelete
  4. Property Dealers in Pitampura-http://www.propertydealerontips.com/Property+Dealers-in-Pitampura-Delhi-65556555
    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 www.propertydealerontips.com or by calling 011-6555-6555.

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

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This comment has been removed by the author.

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

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. 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

    ReplyDelete
  13. 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

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. Kudos to your effort for providing such relevant content. We deal in providing the best residential and commercial properties as we are the bestReal estate company in Lucknow

    ReplyDelete