Tuesday, November 26, 2013

Benefits to using Spring Templates 

(JdbcTemplate, JmsTemplate, RestTemplate, 

JpaTemplate, JdoTemplate, JndiTemplate)

What are the enumerated benefits to using the Spring template classes such as RestTemplate,JdbcTemplateJpaTemplateJdoTemplateJndiTemplate JdoTemplate etc?
I'm looking for the general benefits of using these classes as best practice design, for example the thread safeness of these classes. Or if someone could point me to the section of the Spring documentation that discusses the same I would accept that as well.
share|improve this question
add comment


  1. Less boilerplate code
  2. More cohesive exceptions handling (e.g. JDBC drivers checked exceptions are translated to meaningful runtime exceptions)
  3. Easier and uniform configuration (almost everything can be configured in Spring context files)
  4. Automatized resource management (like, for instance, closing DB connections)
Note, that not all templates are equally useful. For instance jdbcTemplate is a pure gem, I can't live without that (mostly because JDBC is not the best interface one can imagine), on the other hand jpaTemplate does not bring so many advantages.
Note, that if you use given template, your code becomes dependent on Spring interfaces, so Spring is not only dependency mechanism for your application, but becomes also part of it - Spring is no longer easily replacable with something else (Google Guice, CDI). However, given the fact Spring is a pretty good peace of code, I would not be worried about that aspect.
share|improve this answer
add comment