简体   繁体   中英

Spring property resolution in Mule Groovy script

I'm trying to access Spring initialized properties within a Mule Groovy script without success.

Loading the property file in Mule application XML:

<context:property-placeholder location="application.properties"/>

Contents of property file:

key=value

Accessing the property from inside the application XML works fine:

<logger message="key: ${key}"  level="INFO" doc:name="Logger" />

Produces the following output:

key: value

Attempting the same in a groovy script:

log.info "key: ${key}"

Results in exception:

Exception stack is:
1. No such property: key for class: Script1 (groovy.lang.MissingPropertyException)
  org.codehaus.groovy.runtime.ScriptBytecodeAdapter:53 (null)
2. groovy.lang.MissingPropertyException: No such property: key for class: Script1 (javax.script.ScriptException)
  org.codehaus.groovy.jsr223.GroovyScriptEngineImpl:326 (http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/script/ScriptException.html)
3. Failed to invoke ScriptComponent{main-flow.component.32928685}. Component that caused exception is: ScriptComponent{main-flow.component.32928685}. Message payload is of type: ContentLengthInputStream (org.mule.component.ComponentException)
  org.mule.component.AbstractComponent:144 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/component/ComponentException.html)
********************************************************************************
Root Exception stack trace:
groovy.lang.MissingPropertyException: No such property: key for class: Script1
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
at Script1.run(Script1.groovy:18)
<snip>

I'm assuming that all properties are available somewhere within the Groovy script context references available to the script, but I've found no documentation on how to navigate to them. I've looked through the JavaDocs, but have been unable to piece together the proper method for Spring initialized property resolution. Any help is greatly appreciated.

Thanks, Steve

I found the answer to my question in the following post . In short,

Spring property placeholders are resolved at configuration time and not stored anywhere, so they cant be loaded afterwards.

If you need to store it you can always inject them into a bean and retrieve that from the registry.

Based on the answer provided above, this behavior may have changed in newer releases of the Mule runtime, but my experience using version 3.5.4 is consistent with the other post's description of how Spring properties are evaluated by the MEL interpreter.

  • Steve

Which version of Mule runtime are you using ?
I am able to get the value from properties file using following example in Mule runtime 3.7.3:-

 <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="teFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
          <logger message="key: ${key}"  level="INFO" doc:name="Logger" />
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy"><![CDATA[
            System.out.print("In Groovy Script  "+${key}+"\n");
            log.info "In Groovy logger: ${key}";
            ]]></scripting:script>
        </scripting:component>   
  </flow>

application.properties contains:-

key=555

And the output is in the Mule console as follows:-

在此处输入图片说明

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