DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. DRY Your Spring Beans Configuration File

DRY Your Spring Beans Configuration File

Nicolas Fränkel user avatar by
Nicolas Fränkel
CORE ·
Jan. 21, 13 · Interview
Like (0)
Save
Tweet
Share
4.95K Views

Join the DZone community and get the full member experience.

Join For Free

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…

Note: 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.

Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Public Cloud-to-Cloud Repatriation Trend
  • Spring Boot Docker Best Practices
  • Using QuestDB to Collect Infrastructure Metrics
  • Promises, Thenables, and Lazy-Evaluation: What, Why, How

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: