/ DRY, SPRING

DRY your Spring Beans configuration file

It’s always when you discuss with people that some things that you (or the people) hold for an evidence seems to be a closely-held secret. That’s what happened this week when I tentatively showed a trick during a training session that started a debate.

Let’s take an example, but the idea behind this can of course be applied to many more use-cases: imagine you developed many DAO classes inheriting from the same abstract DAO Spring provides you with (JPA, Hibernate, plain JDBC, you name it). All those classes need to be set either a datasource (or a JPA EntityManager, a Spring Session, etc.). At your first attempt, you would create the Spring beans definition file a such:

<bean id="dataSource" ... /><!-- Don't care how we obtain dataSource -->
<bean id="playerDao" class="ch.frankel.blog.app.persistence.dao.PlayerDao">
    <property name="dataSource" ref="dataSource" />
</bean>
<bean id="matchDao" class="ch.frankel.blog.app.persistence.dao.MatchDao">
    <property name="dataSource" ref="dataSource" />
</bean>
<bean id="stadiumDao" class="ch.frankel.blog.app.persistence.dao.StadiumDao">
    <property name="dataSource" ref="dataSource" />
</bean>
<bean id="teamDao" class="ch.frankel.blog.app.persistence.dao.TeamDao">
    <property name="dataSource" ref="dataSource" />
</bean>
<bean id="competitionDao" class="ch.frankel.blog.app.persistence.dao.CompetitionDao">
    <property name="dataSource" ref="dataSource" />
</bean>
<bean id="betDao" class="ch.frankel.blog.app.persistence.dao.BetDao">
    <property name="dataSource" ref="dataSource" />
</bean>

Notice a pattern here? Not only is it completely opposed to the DRY principle, it also is a source for errors as well as decreasing future maintainability. Most important, I’m lazy and I do not like to type characters just for the fun of it.

Spring to the rescue. Spring provides the way to make beans abstract. This is not to be confused with the abstract keyword of Java. Though Spring abstract beans are not instantiated, children of these abstract beans are injected the properties of their parent abstract bean. This implies you do need a common Java parent class (though it doesn’t need to be abstract). In essence, you will shorten your Spring beans definitions file like so:

<bean id="dataSource" ... /><!-- Don't care how we obtain dataSource -->
<bean id="abstractDao" class="org.springframework.jdbc.core.support.JdbcDaoSupport" abstract="true">
    <property name="dataSource" ref="dataSource" />
</bean>
<bean id="playerDao" class="ch.frankel.blog.app.persistence.dao.PlayerDao" parent="abstractDao" />
<bean id="matchDao" class="ch.frankel.blog.app.persistence.dao.MatchDao" parent="abstractDao" />
<bean id="stadiumDao" class="ch.frankel.blog.app.persistence.dao.StadiumDao" parent="abstractDao" />
<bean id="teamDao" class="ch.frankel.blog.app.persistence.dao.TeamDao" parent="abstractDao" />
<bean id="competitionDao" class="ch.frankel.blog.app.persistence.dao.CompetitionDao" parent="abstractDao" />
<bean id="betDao" class="ch.frankel.blog.app.persistence.dao.BetDao" parent="abstractDao" />

The instruction to inject the data source is configured only once for the abstractDao. Yet, Spring will apply it to every DAO configured as having the it as its parent. DRY from the trenches…​

If you use two different data sources, you’ll just have to define two abstract DAOs and set the correct one as the parent of your concrete DAOs.
Nicolas Fränkel

Nicolas Fränkel

Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Also double as a trainer and triples as a book author.

Read More
DRY your Spring Beans configuration file
Share this