Use DataWeave to access Mule application properties
Main points:
- DataWeave script can reference application properties
- DataWeave expressions can reference properties in 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:
- Create the property file in which the application properties are defined
- Create a Configuration Property to reference the location of the file
- Use property placeholders in the applications 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 both secure and insecure properties. In figure 1 the DataWeave script references secure and insecure application property values.
{ "Host":p('db.host'), "Port":p('db.port'), "User":p('db.user'), "Database":p('db.database'), "Password":p('secure::db.password') }
Figure 1: Referencing secure and insecure properties
The output is shown in figure 2.
{ "Host": "db.examples.com", "Port": "3306", "User": "admin", "Database": "products", "Password": "Password123" }
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.

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 these 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.
Leave a Reply