Property Files in Mule 4
It's best practice to centralize the management of properties than hard code values at the location of use. This allows for the reuse of values and better maintenance.
Join the DZone community and get the full member experience.
Join For FreeMain Points:
- Property files allow the centralization of properties.
- Property placeholders used instead of hard coding.
- Property file location must be configured in global elements.
- Property files can be constructed in Java or YAML format.
Centralization of Application Properties
It is best practice to centralize the management of properties rather than hard code values at the location of use. This allows for the reuse of values and better maintenance. Instead of changing the property in multiple places (if the value has been hardcoded), 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:
- The property file in which the properties are defined.
- The Configuration Property in the application's global elements.
- The use of the property placeholders in the application configuration.
The Property File
The property file is a text file that contains the key/value pair representing the property value and its property placeholder name. The file can be either a Java property format file or a YAML file format.
Figure 1: Java-style property file format.
db.host=db.examples.com db.port=3306 db.user=admin db.password=Password123 db.database=products
Figure 2: YAML-style property fie format.
db: host: db.examples.com port: "3306" user: admin password: Password123 database: products
Java-style property files have the .properties
extend while the YAML-style property files have the .yaml
file extension. The two formats in figures 1 and 2 defined the same properties.
The Property File Location
The property files should be located on the classpath. 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 classpath in the src/main/resources
directory the configuration of the Configuration Property element looks like figure 3.
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.
It is also possible to reference Mule application properties using the Data Weave function Mule::p
.
Secure and Encrypted Properties
Typically, Mule applications require 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, they can also be hidden from view in CloudHub.
Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments