Announcement Announcement Module
Collapse
No announcement yet.
read config file into a string property? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • read config file into a string property?

    Is it possible to read in the contents an an XML file into a string and then use that string value as a parameter to a Spring bean?

    I have a problem with a jms service (Mantaray) defined as a Spring bean that uses an external config file to store its own properties. By default the jms service is passed the address of this config by setting a system property (i.e. 'mantaConfig'). The alternative is pass the xml contents of this config file as a string property to the jms bean.

    For ease of deployment I'd prefer not to set the system property outside of the Spring application. Hence, I'm looking for some ideas on how to address either of the following...
    • when the spring app initializies have the 'mantaConfig' system property set to the absolute path of a defined xml config file; or preferably...
    • load the contents of the xml file into a string then pass that string as a bean property to the jms bean.
    Ideas?

  • #2
    Do you need to use the external config file or can you just use CDATA tags in your Spring config file to inject the String? Below is an example using CDATA.

    Code:
    <bean class="com.example.JmsBean">
    	<property name="value">
    		<value><![CDATA[<jms><config>value</config></jms>]]></value>
    	</property>
    </bean>
    You could use a factory to create the bean. An example with commons-io on the classpath is below.

    Code:
    <bean class="com.example.JmsBean">
    	<property name="value">
    		<bean class="org.apache.commons.io.IOUtils" factory-method="toString">
    			<constructor-arg type="java.io.InputStream" value="classpath:jms-config.xml"/>
    		</bean>
    	</property>
    </bean>
    HTH,
    Rob

    Comment


    • #3
      Many thanks... your factory bean idea is brilliant.

      Code:
         <bean id="jmsFactory" class="org.mr.api.jms.MantaConnectionFactory" >
            <property name="configuration">
               <bean class="org.apache.commons.io.IOUtils" factory-method="toString">
                  <constructor-arg type="java.io.InputStream" value="classpath:mantaray_config.xml"/>
               </bean>
            </property>
         </bean>
      I have one wrinkle to cure. There are two ways to push the xml file contents into this jms bean and both use the same named 'configuration' property...
      * the first has 'configuration' as a string and will take the xml file content
      * the second has 'configuration' as an XML Element and will take a DOM model as its parameter.

      When I run a standalone test app then the 'IOUtils' factory bean idea works perfectly, as the spring bean application context file parser finds 'configuration' as a string property.

      However, when I run my Spring web app the same code fails as the bean xml parser is finding configuration as the Element parameter, see...

      Code:
      aused by: java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [org.w3c.dom.Element] for property 'configuration': no matching editors or conversion strategy found
      	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
      Is it possible to define the 'configuration' type parameter type as string only?

      Comment


      • #4
        I found a work around, where I create a new sub class of my own that uses a unique property name, like...

        Code:
        public class SkapConnectionFactory extends MantaConnectionFactory {
        
           private static final long serialVersionUID = -6425796291574414062L;
        
           public SkapConnectionFactory() {
           }
        
           public SkapConnectionFactory(String s) {
              super(s);
           }
           
           public void setConfigAsString(String aStr) throws ParserConfigurationException, SAXException, IOException {
              super.setConfiguration(aStr);
           }
        
        }
        
           <bean id="jmsFactory" class="com.eis.jms.mantaray.SkapConnectionFactory" >
              <property name="configAsString">
                 <bean class="org.apache.commons.io.IOUtils" factory-method="toString" >
                    <constructor-arg type="java.io.InputStream" value="classpath:mantaray_config.xml"/>
                 </bean>
              </property>
           </bean>

        Comment

        Working...
        X