Mule 3 - Loading from .properties file
Asked Answered
J

5

5

I'm using Mule 3 to query a database using JDBC, and I'd like to modify the query depending on input from a .properties file. I have this in my xml...

<context:property-placeholder location="C:\path\to\file\settings.properties" />

Getting the following exception...

Exception in thread "main" org.mule.module.launcher.DeploymentInitException: SAXParseException: The prefix "context" for element "context:property-placeholder" is not bound.

Do I need to include some special .xsd file?

Julijulia answered 25/6, 2013 at 17:52 Comment(0)
A
7

Add the xmlns namespace prefix and schema location to your Mule config mule element tag.

Prefix:

xmlns:context="http://www.springframework.org/schema/context"

SchemaLocation:

http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd

It should look like as below.

Eg:

<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:spring="http://www.springframework.org/schema/beans"      
    xmlns:http="http://www.mulesoft.org/schema/mule/http" 
    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.3/mule.xsd
        http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/3.3/mule-http.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd      
        ">


<context:property-placeholder location="C:/path/to/file/settings.properties" />


  ...........  Other stuff



</mule>
Allergen answered 25/6, 2013 at 19:30 Comment(3)
Thanks. That seems to have fixed the namespace issue but I'm getting a FileNotFound exception when the file clearly exists. Have you experienced this issue before?Julijulia
Made a new question about this issue, here is the link #17327283 Please see if you can resolveJulijulia
There is an answer posted for your question. Please try it. That should solve it.Allergen
D
4

I had the same issues and fixed it. Here what I did.

  1. Kept all .properties under src/main/resources
  2. < context:property-placeholder location="file.dev.properties,file.stage.properties" />
  3. Keeping all the files in classpath was a challenge. So Goto your project folder, Open .classpath file in text pad and add the below line

    < classpathentry 
      including="file.dev.properties|file.prod.properties|file.stage.properties"
      kind="src" path="src/main/resources"/ >
    
  4. Save, refresh and it works.
Dyak answered 7/10, 2013 at 19:19 Comment(0)
S
0

Use the following xsd in xsi:schemaLocation --

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd

Schlegel answered 25/6, 2013 at 18:21 Comment(1)
I'm having the same exception. I should add that I'm placing the placeholder inside the mule tags itself, i.e. <mule><context:property-placeholder location="C:\path\to\file\settings.properties" /><other stuff></mule Should I be placing it inside an object?Julijulia
A
0

The other answers covered the namespace issue, but I'll add that I found that the context:property-placeholder tag needed to be between "spring:beans" tags. Here's an example that presumes that the properties file sets a property named "jmsBrokerURL":

<mule xmlns="http://www.mulesoft.org/schema/mule/core" version="EE-3.4.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      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"
      xsi:schemaLocation="
          http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
          http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <spring:beans>
        <context:property-placeholder location="C:/path/to/file/settings.properties" />
        <spring:bean name="myConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
            <spring:property name="brokerURL" value="${jmsBrokerURL}" />
        </spring:bean>
    </spring:beans>

    <flow name="MyFlow" doc:name="MyFlow">
        <!-- Flow configuration here. -->
    </flow>

</mule>

An alternate method of reading properties (and one I prefer) is to use the Spring "util:properties" tag to read properties into a Properties bean which you then refer to using Spring EL. Watch out in this case that you use the Spring EL "#{}" notation instead of "${}" to reference the object and its variables. Here's the above example modified for that technique:

<mule xmlns="http://www.mulesoft.org/schema/mule/core" version="EE-3.4.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:spring="http://www.springframework.org/schema/beans"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="
          http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <spring:beans>
        <util:properties id="myConfig"  location="C:/path/to/file/settings.properties" />
        <spring:bean name="myConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
            <spring:property name="brokerURL" value="#{myConfig.jmsBrokerURL}" /> <!-- Note the pound (hash) symbol. -->
        </spring:bean>
    </spring:beans>

    <flow name="MyFlow" doc:name="MyFlow">
        <!-- Flow configuration here. -->
    </flow>

</mule>

I like this latter approach mainly because I can deal with multiple properties files and included application context files more easily. The context:property-placeholder tag can be problematic when dealing with either multiple property files or when including an application context file within another.

Ashaashamed answered 25/6, 2013 at 19:59 Comment(0)
F
0

Just put the properties file under resource folder and

Use this " classpath:settings.properties " in the property-placeholder and it will work ...

Fettling answered 7/7, 2013 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.