JDO and EJB join forces: The cat is out of the bag Information on Java Closures from Guy Steele of Sun
Sep 27

Consilidating Spring Config with Groovy

Tech Add comments

When working with Spring, have you noticed yourself doing a lil copy/paste in the applicationContext.xml?

I have seen a fair share on different projects.

For example, you wrap your DAO model with the transaction proxy, and the config is the same for each.

One of them looks something like this:

<bean id="UserDAO"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="userDAOTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="set*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<bean id="userDAOTarget" class="com.foo.dao.UserDAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>

Instead of duplicating the code for every object in the model it would be great if there was Groovy support so you could do something like this:

modelBeans = ['User', 'Auction', 'Bid', ..]
txRequiredMethods = ['add*', 'set*', 'update*']

modelBeans.each { | beanName |
builder = new SpringMarkupBuilder()
markup = {
bean(id="${beanName}DAO", class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean") {
property(name: 'transactionManager') {
ref(local: 'transactionManager')
}

property(name: 'target') {
ref(local: "${beanName}DAOTarget");
}

property(name: 'transactionAttributes') {
props {
txRequiredMethods.each { | txMethod |
prop(key: txMethod, value: 'PROPAGATION_REQUIRED')
}
}
}
}

bean(id="${beanName}DAOTarget", class="com.foo.dao.${beanName}DAOImpl") {
property(name: 'sessionFactory') {
ref(local: "sessionFactory");
}
}
}
}

Of course, many other solutions would help out here.

UPDATE: And Colin has one :)

As Matt mentioned in a comment, Colin Sampaleanu has a great solution.

2 Responses to “Consilidating Spring Config with Groovy”

  1. Carlos Sanchez's Weblog Says:

    Avoid duplicated info in the Spring application context

    Some configuration is shared across many projects and duplication must be avoided, e.g. for hibernate configuration this can be achieved using ArrayList for properties of type List and a custom class HibernateProperties for properties of type Properties

  2. freakish cocks Says:

    index10.html index11.html http://ciaolin.info/teens/index12.html
    index12.html index13.html http://ciaolin.info/teens/index14.html
    index14.html index15.html http://ciaolin.info/teens/index15.html
    [url=http://ciaolin.info/teens/index10.html]index10.html[/url] [url=http://ciaolin.info/teens/index11.html]index11.html[/url] http://ciaolin.info/teens/index11.html
    free black cock [url=http://ciaolin.info/teens/free-black-cock.html]free black cock[/url] http://ciaolin.info/teens/free-black-cock.html
    freakish cocks [url=http://ciaolin.info/teens/freakish-cocks.html]freakish cocks[/url] http://ciaolin.info/teens/freakish-cocks.html

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: What are the first four letters in the word British?