简体   繁体   中英

Mule 3 - Loading from .properties file

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?

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>

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.

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.

Just put the properties file under resource folder and

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM