DZone
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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Externalize Microservice Configuration With Spring Cloud Config
  • A Guide To Build, Test, and Deploy Your First DApp
  • How To Build Web Service Using Spring Boot 2.x
  • Visually Designing Views for Java Web Apps

Trending

  • Microservices Architecture in Production: 7 Engineering Decisions That Determine Success or Failure
  • This One Spring Data JPA Pattern Cleaned Up to 3 Years of Repository Debt
  • What Nobody Tells You About Running AI Models in Docker
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector

Access Mule Application Properties With DataWeave

In a DataWeave script, you can reference an application property defined in a properties files by using the p function of the dw::Mule module.

By 
Alex Theedom user avatar
Alex Theedom
·
Mar. 07, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
18.2K Views

Join the DZone community and get the full member experience.

Join For Free

Main Points

  • DataWeave script can reference application properties.
  • DataWeave expressions can reference properties in the configuration.

Configure a Property File

Firstly, you will need to configure a property file in which application properties are defined. There are steps parts to follow:

  1. Create the property file in which the application properties are defined.
  2. Create a Configuration Property to reference the location of the file.
  3. Use property placeholders in the application configuration.

You can find more about this in my blog post How to configure Property Files in Mule 4.

Reference Properties in DataWeave Scripts

In a DataWeave script, you can reference an application property defined in a properties files by using the p function of the dw::Mule module. The module is imported by default. You can refer to both secure and insecure properties. In figure 1 the DataWeave script references secure and insecure application property values.

JSON
 




xxxxxxxxxx
1


 
1
{
2
  "Host":p('db.host'),
3
  "Port":p('db.port'),
4
  "User":p('db.user'),
5
  "Database":p('db.database'),
6
  "Password":p('secure::db.password')
7
}



Figure 1: Referencing secure and insecure properties.

The output is shown in figure 2.

JSON
 




xxxxxxxxxx
1


 
1
{
2
  "Host": "db.examples.com",
3
  "Port": "3306",
4
  "User": "admin",
5
  "Database": "products",
6
  "Password": "Password123"
7
}



Figure 2: Output from DataWeave script in Figure 1.

Reference Properties in Mule Configurations

The p function can also be used instead of the property placeholder to reference application properties in the configuration of Mule connectors and event processors. In figure 3 the database connector is configured with using the p function.

Figure 3: Use of the Mule::p function to reference application properties

Figure 3: Use of the Mule::p function to reference application properties.

Ensure that the mode is switch to expression and that the DataWeave script is encapsulated between #[...]. It is worth noting that not all configurations present the options of literal or expression mode and in this circumstance, you would need to enter the full DataWeave expression, as shown in figure 3 for the password configuration.

Note though that this manner of configuration is not expected by Anypoint Studio and will display an error in Studio's Problems tab stating that 'Configuration used for Metadata fetch cannot be dynamic.' This is not a Mule application error and will not prevent your application from executing but will always be present in Studio.

Referencing Encrypted Properties

It is usual to encrypt sensitive Mule application properties such as passwords. Discover how to encrypt Mule 4 properties and hide properties from view in CloudHub in these two blog posts on Mule application properties.

Property (programming) application

Published at DZone with permission of Alex Theedom. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Externalize Microservice Configuration With Spring Cloud Config
  • A Guide To Build, Test, and Deploy Your First DApp
  • How To Build Web Service Using Spring Boot 2.x
  • Visually Designing Views for Java Web Apps

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook