Search code examples
anypoint-studio

How to store data from a .properties file in a variable in Mulesoft Anypoint Studio?


I've got a few .properties files containing some data. How do I write a specific value of a property from one of the property files to a variable?


Solution

  • There are the following three approaches to perform on this problem statement.

    1.Reading a properties file using ${Key} expression
    2.Reading a properties file using ![p[‘Key’]] expression
    3.Reading a properties file using p() function from DataWeave
    
    While according to your need, I have used the first approach. Please go through the following code. I think this will resolve your problem.
    
    
    
        <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
            <context:property-placeholder location="propread.properties"/>
            <flow name="propreadFlow">
                <http:listener config-ref="HTTP_Listener_Configuration" path="/propread" doc:name="HTTP"/>
                <logger message="&quot;Flow Started&quot;" level="INFO" doc:name="Logger"/>
                <set-variable variableName="prop" value="${prop.read}" doc:name="Variable"/>
                <logger message="#[flowVars.prop]" level="INFO" doc:name="Logger"/>
            </flow>
        </mule>
    
    Please create one property file under src/main/resources with the name propread.properties and copy this  ( prop.read=HelloMule ) into the file and run the above code. You will get the solution.