Use Arid to make your Spring configuration extra DRY - No Fluff Just Stuff

Use Arid to make your Spring configuration extra DRY

Posted by: Craig Walls on May 4, 2007

In case you missed it on TSS or Chris Richardson's blog...Once a Spring application gets large enough, you'll probably find that there are several beans configured in your Spring application context that are pretty much the same. For example:

<bean id="customerDao" class="com.habuma.dao.CustomerDaoImpl">
  <property name="dataSource" ref="dataSource" />
</bean>

<bean id="productDao" class="com.habuma.dao.ProductDaoImpl">
  <property name="dataSource" ref="dataSource" />
</bean>

<bean id="orderDao" class="com.habuma.dao.OrderDaoImpl">
  <property name="dataSource" ref="dataSource" />
</bean>

<bean id="vendorDao" class="com.habuma.dao.VendorDaoImpl">
  <property name="dataSource" ref="dataSource" />
</bean>

<bean id="categoryDao" class="com.habuma.dao.CategoryDaoImpl">
  <property name="dataSource" ref="dataSource" />
</bean>

<bean id="specialDao" class="com.habuma.dao.SpecialDaoImpl">
  <property name="dataSource" ref="dataSource" />
</bean>

Notice a pattern?

This was just a contrived example, but you've probably seen something very similar (or worse) in your Spring application. Sure, you could use auto-wiring to cut down on the XML, but that would only help out a little bit. You'd still have to declare each bean. If only there were some way to not only auto-wire, but to also auto-declare...

Enter Arid POJOs, a Spring configuration extension by Chris Richardson (the author of POJOs in Action) that lends to DRY bean configuration. Using Arid POJOs, you can dramatically cut down on the amount of XML in your Spring configuration file by declaring rules for auto-declaring beans. For example, the DAO beans above can be more succinctly declared with the following XML:

<arid:define-beans
    package="com.habuma.dao"
    autowire="byType" />

This tiny bit of XML will automatically create beans for all classes in the "com.habuma.dao" package, auto-wiring their properties "byType". Only 3 short lines (could be fit on one line) replace 18 lines of XML for 6 DAOs. And here's the kicker: If I add another DAO (or even 1000 of them) to the "com.habuma.dao" package, I don't have to add any more XML! Those three lines will handle all of them, so long as they're in the "com.habuma.dao" package.

Chris has written more about Arid on his blog. Check it out!

Craig Walls

About Craig Walls

Craig Walls is a Principal Engineer, Java Champion, Alexa Champion, and the author of Spring AI in Action, Spring in Action, and Build Talking Apps. He's a zealous promoter of the Spring Framework, speaking frequently at local user groups and conferences and writing about Spring. When he's not slinging code, Craig is planning his next trip to Disney World or Disneyland and spending as much time as he can with his wife, two daughters, 1 bird and 2 dogs.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »