This post originated from an RSS feed registered with Java Buzz
by dion.
Original Post: Consilidating Spring Config with Groovy
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
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.