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.

26 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. Residential Property in Yamuna Expressway
    :-very nice information, we are glad to read this news or post,

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

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

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

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

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

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

    ReplyDelete
  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

    ReplyDelete
  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

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

    ReplyDelete
  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 think4buysale.in 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 - Think4buysale.in

    ReplyDelete
  19. Office space for rent inpatparganj industrial area.www.think4buysale.in 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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  22. Sunshine Nest dealing with the real estate firm in gurgoan and provide the 1 BHK ready to move in flat for sale in gurgoan. They provide the 1 BHK semi furnished in gurgoan and 1 BHK fully furnished on rent in gurgaon.
    1 bhk flat in Gurgaon

    Sunshine Nest dealing with the real estate firm in gurgoan and provide the 2 BHK ready to move in flat for sale in gurgoan. They provide the 2 BHK apartments for rent in gurgoan. You can get the 2 BHK apartments in golfcourse road gurgoan.
    2 bhk flat in gurgaon

    Sunshine Nest dealing with 3bhk flats in gurgaon and provide the 3 BHK ready to move in flat for sale in gurgoan. They provide the 3 BHK furnished flats in gurgoan at affordable 3 bhk flat in gurgaon price. You can get the rent and sale facilities for, 3 bhk flat rent and 3 bhk for sale in gurgaon.
    3 bhk flat in gurgaon

    Dlf Primus gurgoan is a real estate property of which sunshine nest dealing. Dlf Primus floor plan is available and Dlf primus is on re sale so you can book your property at the affordable dlf primus price rate.
    DLF Primus Gurgaon

    ReplyDelete
  23. Shristi homes are the property dealer in Uttam Nagar who provides 1 BHK Flat in Uttam Nagar, latest projects in Uttam Nagar which includes 1BHK apartments/ Flats at fair Uttam Nagar Property Rates.
    1 bhk flat in uttam nagar

    Shristi homes is a trusted property dealer in Uttam Nagar dealing with the 2 BHK,2 BHK Flat in Uttam Nagar Independent/Builder Floor in Uttam Nagar in proper residential Area and ready to move in Flat in Uttam Nagar.
    2 bhk flat in uttam nagar

    Fully furnished apartments for sale in uttam nagar provided by the shristi homes and dealing with the 3 bhk builder floor in uttam nagar west, and 3 BHK Flat in Uttam Nagar
    3 bhk flat in uttam nagar

    ReplyDelete