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

  • Agentic Test Creation: From Plain-Language Requirements to End-to-End Test Cases
  • Key ML Classification Metrics You Need to Understand
  • Avoiding AI Debt as a Team
  • Ingesting Public Data That Fails Silently: Patterns From a TypeScript and Cloudflare Workers Pipeline

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

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
·
Mar. 07, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
18.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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