DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Expressing the Spring Expression Language via Mule

Expressing the Spring Expression Language via Mule

Mule easily uses the Spring Expression Language (SpEL) in its application. We will be creating a small Mule application to demonstrate the usage of SpEL in a Mule app.

Anirban Sen Chowdhary user avatar by
Anirban Sen Chowdhary
·
Dec. 19, 16 · Integration Zone · Tutorial
Like (3)
Save
Tweet
30.16K Views

Join the DZone community and get the full member experience.

Join For Free

We all might be familiar with the Spring Expression Language (SpEL), which is one of the powerful expression languages that supports querying and manipulating an object graph at runtime.

There are also several other expression languages available like OGNL, MVEL, MEL, and JBoss EL, but SpEL is undoubtedly one of the better performer and was created to provide the Spring community with a single well supported expression language that can be used across all the Spring products.

Mule has an old relationship with Spring and supports the Spring framework for developing Java applications. Mule has the advantage of creating and managing Spring application contexts for our Mule application. Mule XML configuration file uses Spring 4.1.6’s ability to create bean definition profiles.

Though Mule has its own expression language called MEL, which is based on the top of MVEL, we will see in this article how Mule easily uses the Spring Expression Language (SpEL) in its application. We will be creating a small Mule application to demonstrate the usage of SpEL in a Mule app.

Let’s start creating our application with following Java class:

package com.service;

public class PathAddress {

       private String path;

       private String basePath;

       public String getPath() {

              return path;

       }

       public void setPath(String path) {

              this.path = path;

       }


       public String getBasePath() {

              return basePath;

       }

       public void setBasePath(String basePath) {

              this.basePath = basePath;

       }



       public String myTestMethod() {

              return "Hi, This is a test method !";

       }

}

We can see that we have defined two String values, path and basePath, that we will set in the URL of our application.

Let's look into our Mule flow:

<spring:beans>

     <spring:bean id="pathversion" class="com.service.PathAddress">

         <spring:property name="path" value="#{'test'}"/>

         <spring:property name="basePath" value="#{ systemProperties['base.path'] }"/>

     </spring:bean>

   </spring:beans>

    <http:listener-config name="HTTP_Listener_Configuration" basePath="#{pathversion.getBasePath()}" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>


     <flow name="Flow">

         <http:listener config-ref="HTTP_Listener_Configuration" path="#{pathversion.getPath()}" doc:name="HTTP"/>

        <set-payload doc:name="Set Payload" value="#{pathversion.myTestMethod()}"/>

         <parse-template location="response.html" doc:name="Parse Template"/>

      </flow>

We can see here that we have defined the Spring bean and passing the values of the variables path and basePath from the bean. We have used SpEL to set the value of the variable elements.

For the path element, we have used SpEL to set the value directly as a string, whereas for the basePath element, we have used SpEL to set the value from the environment variable, which is defined in the app.properties file of our application:

base.path=application

Now, if you look in the XML, you can see that we have used SpEL to assign the values in the basePath for our HTTP listener connector as well as in our path of HTTP listener endpoint in the flow.

Further, we are calling our method myTestMethod() of the Java class using the SpEL.

At the end, we have added a basic HTML file in src/main/resources folder and call it in our flow using parse-template to spice up our response:

<html>

<head>

<title>Spring Expression Language Sample</title>

</head>

<body>

<center>

<b><font size="30" color="red">#[payload]</font></b>

</center>

</body>

</html>

Testing our Application

I we now deploy our application in server and test our application with this URL, we can see the result in the browser:

Image title

Conclusion

As we can see, we can easily use the powerful Spring Expression Language in our Mule application easily. Although Mule has its own MEL expression, we can implement SpEL side-by-side in our application as Mule is built on Spring and it offers flexibility to integrate this SpEL easily in our code.

Spring Framework application

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Benefits of Electronic Data Interchange
  • NativeScript vs. Flutter: A Comparison for Tech Businesses and Entrepreneurs
  • A Complete Guide to Generated Columns in MySQL
  • SQL GROUP BY and Functional Dependencies: a Very Useful Feature

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo