Read and use properties file in Mule 3

Reading and using a properties file in an application is a very important operation, making our application usable in many different environments with different configuration parameters. In Mule 3, reading and using properties files will involve much use of Spring framework to manipulate properties files. How is it in details? Let’s find out in this tutorial together!

First, I will create a Maven Mule project as an example:

Read and use properties file in Mule 3

Our application will simply include an HTTP Listener Connector for listening to requests from users and a Huong Dan Java Logger component to retrieve information from properties file and print to the console, as follows:

Read and use properties file in Mule 3

Inside:

HTTP Listener Connector is configured as follows:

Read and use properties file in Mule 3

General Settings:

Read and use properties file in Mule 3

Huong Dan Java Logger component will be initially configured as follows:

Read and use properties file in Mule 3

Connector Configuration:

Read and use properties file in Mule 3

Now I will define a properties file located in the src/main/resource directory named application.properties with the following simple content:

Now how can we load the value of author.name property in application.properties file and use it to print the words “Hello Khanh” with Huong Dan Java Logger component?

To do this, you first need to learn how Spring framework works with properties files. You can refer here. We can apply the way that Spring manipulates properties files in Mule 3 backwards as follows:

In the file mule-esb-properties-file-mule3.xml, please declare context namespace of Spring as follows:

Then, use the context namespace to read properties file as follows:

Now similar to Spring framework, you can declare to use author.name property in application.properties file as follows:

The result when you run the application and the request to http://localhost:8081/properties-file will look like this:

Read and use properties file in Mule 3

Similar to Spring framework, you can declare many properties files in the location attribute and can also declare the ignore-resource-not-found attribute to ignore non-existent properties, as follows:

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment