Wednesday, September 7, 2011

Spring Grails Bean Builder Confusion

Spent hours today trying to convert some traditional Spring XML config into more dynamic Grails Bean Builder script to allow dynamic config based on the environment that the webapp is deployed (Windows vs. non-Windows/*nix).  This was NOT a Grails project so I first had to include the dependency in the build.gradle file.

    compile "org.grails:grails-spring:1.3.7"


Here is a snippet of the Spring XML I was trying to convert:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

    <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory">
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="${jms.brokerUrl}"/>
            </bean>
        </property>
        <property name="sessionCacheSize" value="10"/>
        <property name="cacheProducers" value="false"/>
    </bean>

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory"><ref local="connectionFactory"/></property>
    </bean>
...
</beans>

All was okay until I tried to deal with the 'property-placeholder' for the config.properties.  I tried lots of stuff and on a whim tried this very counterintuitive thing with a Java String for the property key that looks like a Groovy String that in the BeanBuilder should be interpolated:

beans {
    xmlns context:"http://www.springframework.org/schema/context"

    context.'property-placeholder'('location':'classpath:config.properties')

    activeMqConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) {
        brokerURL = '${jms.brokerUrl}' // Yes, this is meant to be a java.lang.String
    }

    jmsFactory(org.springframework.jms.connection.CachingConnectionFactory) {
        targetConnectionFactory = activeMqConnectionFactory
    }

    jmsTemplate(org.springframework.jms.core.JmsTemplate) {
        connectionFactory = jmsFactory
    }
...
}


Yes boys and girls...


'${jms.brokerUrl}'

Major brain eff here with something that looks like it should be "${jms.brokerUrl}" with double quotes for Groovy String interpolation or perhaps even simply jms.brokerUrl like a variable attached to a dynamically generated property on the beans object.  I even tried context['jms.brokerUrl']and beans['jms.brokerUrl']to no avail.

I tried the actual solution when it occurred to me that Spring would not be doing interpolation on the ${blah} like Groovy when reading the XML.  For Spring, that means 'use this string as a key to a resource'.
Oyvay.

No comments:

Post a Comment