czwartek, 25 listopada 2010

Don't repeat the Spring beans! (in Apache Camel and OSGi environment)

The 'Don't repeat the DAO!' article is a fundamental one for all Spring/ORM/Java world. The generic DAO pattern works well and is widely used in both commercial projects and open-source implementations.

But what if you only need one class for all of your DAOs? In one of my projects I just needed to put all records from a database view to a file. I decided not to use an ORM, but just Spring's JdbcTemplate, configured by view name and column to use in ORDER BY section. So I started to write Spring XML wiring and realized.... that I'm repeating the Spring beans! Every declaration looked something like:
<bean id="customerDAO" class="">
     <property name="table" value="V_CUSTOMERS"/>
     <property name="orderColumn" value="CUST_ID"/>

  <bean id="contractDAO" class="">
     <property name="table" value="V_CONTRACTS"/>
     <property name="orderColumn" value="CONTRACT_ID"/>

There were about 15 twin beans in my Spring XML files! That looked really ugly...