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.
Join the DZone community and get the full member experience.Join For Free
- 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:
- 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 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.
Figure 1: Referencing secure and insecure properties.
The output is shown in figure 2.
Figure 2: Output from DataWeave script in Figure 1.
Reference Properties in Mule Configurations
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
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.
Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.