News Ticker

Property Files in Mule 4

Main points:

  • Property files allow centralization of properties
  • Property placeholders used instead of hard coding
  • Property file location must be configured in global elements
  • Property files can be constructed as Java or YAML format

Centralisation of application properties

It is best practice to centralise the management of properties rather than hard code values at the location of use. This allows for the reuse of values and the better maintenance. Instead of changing the property in multiple places (if the value has been hard coded), you change the value once in the property file and every location where that property placeholder is used gets to use the new value at runtime.

Setting up a property file

There are three parts to set up when you want to use property files:

  1. The property file in which the properties are defined
  2. The Configuration Property in the application’s global elements
  3. The use of the property placeholders in the applications configuration

The property file

The property file is a text file the contain the key/value pair representing the property value and its property placeholder name. The file can be wither Java property format file or a YAML file format.

db.host=db.examples.com
db.port=3306 
db.user=admin
db.password=Password123
db.database=products

Figure 1: Java-style property file format

db:
  host: db.examples.com
  port: "3306"
  user: admin
  password: Password123
  database: products

Figure 2: YAML-style property fie format

Java-style property files have the .properties extend while the YAML-style property files have the .yaml file extend. The two formats in figures 1 and 2 defined the same properties.

The property file location

The property files should be located on the class path. Typically application property files are located in the src/main/resources directory.

Provide a Configuration Property setting

The Mule application must be configured with the location of the property file. This is done by providing its location in the Configuration Property configuration element in the global elements of the application. If the property file is call properties.yaml and is located on the class path in the src/main/resources directory the configuration of the Configuration Property element looks like figure 3.

Mule Configuration Properties
Figure 3: Configuration Property set with the location of the property file.

The use of the property placeholders

Properties are used by referencing the name of the property enclosed in ${}. For example, the use of the properties set in figures 1 & 2 would look like this ${db.host}, ${db.port}, ${db.user}, ${db.password}, ${db.database} and be used as shown in figure 4.

Figure 4: Using property placeholders in the configuration of a Database connector

It is also possible to reference Mule application properties using the Data Weave function Mule::p.

Secure and encrypted properties

Typically, Mule applications requires the use of secure properties such as passwords. These can be encrypted with an encryption key and used in the same way as insecure properties. Learn how to encrypt Mule 4 properties and hide properties from view in CloudHub.

2 Trackbacks / Pingbacks

  1. Use DataWeave to access Mule application properties
  2. How to Hide CloudHub Properties?

Leave a Reply

%d bloggers like this: