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:
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:
Inside:
HTTP Listener Connector is configured as follows:
General Settings:
Huong Dan Java Logger component will be initially configured as follows:
Connector Configuration:
Now I will define a properties file located in the src/main/resource directory named application.properties with the following simple content:
1 |
author.name=Khanh |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:huong-dan-java-logger-component="http://www.mulesoft.org/schema/mule/huong-dan-java-logger-component" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/huong-dan-java-logger-component http://www.mulesoft.org/schema/mule/huong-dan-java-logger-component/current/mule-huong-dan-java-logger-component.xsd"> <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/> <huong-dan-java-logger-component:config name="Huong_Dan_Java_Logger__Configuration" category="com.huongdanjava" doc:name="Huong Dan Java Logger: Configuration"/> <flow name="mule-esb-properties-file-mule3Flow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/properties-file" allowedMethods="GET" doc:name="HTTP"/> <huong-dan-java-logger-component:log config-ref="Huong_Dan_Java_Logger__Configuration" message="Hello" doc:name="Huong Dan Java Logger"/> </flow> </mule> |
Then, use the context namespace to read properties file as follows:
1 |
<context:property-placeholder location="classpath:application.properties" /> |
Now similar to Spring framework, you can declare to use author.name property in application.properties file as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:huong-dan-java-logger-component="http://www.mulesoft.org/schema/mule/huong-dan-java-logger-component" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/huong-dan-java-logger-component http://www.mulesoft.org/schema/mule/huong-dan-java-logger-component/current/mule-huong-dan-java-logger-component.xsd"> <context:property-placeholder location="classpath:application.properties" /> <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/> <huong-dan-java-logger-component:config name="Huong_Dan_Java_Logger__Configuration" category="com.huongdanjava" doc:name="Huong Dan Java Logger: Configuration"/> <flow name="mule-esb-properties-file-mule3Flow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/properties-file" allowedMethods="GET" doc:name="HTTP"/> <huong-dan-java-logger-component:log config-ref="Huong_Dan_Java_Logger__Configuration" message="Hello ${author.name}" doc:name="Huong Dan Java Logger"/> </flow> </mule> |
The result when you run the application and the request to http://localhost:8081/properties-file will look like this:
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:
1 |
<context:property-placeholder location="classpath:application.properties,classpath:application1.properties" ignore-resource-not-found="true" /> |
kumar
i want to pass those values separately during run time using jenkinsfile
kumar
how to read the properties in Mule4 using Jenkinsfile