20

I'm new to Java. I've only been programming it for about a year. What does Spring mean by the use of templates? In Spring, there is jdbc-templates, jms-templates etc.. What are template classes in java? Are they a special kind of design pattern or what?

Thank you in advance.

Horse Voice
  • 8,138
  • 15
  • 69
  • 120
  • Spring templates are based on the [Template Method design pattern](http://en.wikipedia.org/wiki/Template_method_pattern) – Ori Dar Jan 31 '14 at 22:44
  • @OriDar, `JdbcTemplate` is [not an example](https://stackoverflow.com/questions/33153252/why-is-jdbctemplate-an-example-of-the-template-method-design-pattern) of the Template Method design pattern. That pattern requires subclassing, whereas the doc for `JdbcTemplate` states, "_there should be no need to subclass it._" – jaco0646 Dec 09 '20 at 22:34

3 Answers3

10

Spring templates are a way to eliminate boilerplate code that is needed to correctly use many APIs such as JDBC, JMS, transactions, etc. Boilerplate code is setup and error handling code that needs to be written in order to use correctly a API.

For example in JDBC, for executing a query the template will take care of the all setting of the connection, preparing the statement, releasing the connection after the query is done, handling exceptions all of which is non-trivial and easy to get wrong.

To the template you just need to pass in the query that you want to run, and the rest is taken care by the template.

Take the example on this blog post, a program of 80 lines executing a query in plain jdbc was reduced to 20 lines when using the spring JDBC template.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
Angular University
  • 42,341
  • 15
  • 74
  • 81
10

They are called template as use the Template method pattern.

Basically the idea is define the operation needed to do something in an abstract class or super class then implement a class that use the operation previous defined.

In the case of spring allow that operation that always need to be done for an specific purpose be done automatically, (open connection, obtain for pool, translation, execution, close connection), then user only need to call methods without worries about the previous tasks.

lakshman
  • 2,641
  • 6
  • 37
  • 63
Koitoer
  • 18,778
  • 7
  • 63
  • 86
  • Picked this answer because you mentioned about abstract classes. Now it makes sense how the boiler plate code is taken care of. Via abstract classes is one way. Thank you – Horse Voice Feb 03 '14 at 04:20
  • 1
    `JdbcTemplate` is [not an example](https://stackoverflow.com/questions/33153252/why-is-jdbctemplate-an-example-of-the-template-method-design-pattern) of the Template Method design pattern. That pattern requires subclassing, whereas the doc for `JdbcTemplate` states, "_there should be no need to subclass it._" This answer is wrong. – jaco0646 Dec 09 '20 at 22:28
1

These kind of classes are used to simplify the functionality, letting low level issues asside of, for example, connecting to the database (all the dirty work is done by the jdbcTemplate class).

The JdbcTemplate simplifies the use of JDBC and helps to avoid common errors. It executes core JDBC workflow, leaving application code to provide SQL and extract results. This class executes SQL queries or updates, initiating iteration over ResultSets and catching JDBC exceptions and translating them to the generic, more informative exception hierarchy.

The only thing you need to actually implement are the CallBack methods. Implementing only callback methods let your code clean. Your only concern is to execute your business logic.

Eduardo Briguenti Vieira
  • 4,351
  • 3
  • 37
  • 49
  • Thank you edubriguenti. What are call back methods? Could you provide an example as it relates to jdbs-templates? Thank you – Horse Voice Feb 01 '14 at 18:26
  • Callback methods are methods called by the framework. You just implement them (normally because you implemented an interface), but is the framework or API that actually called the method in the proper moment. – Eduardo Briguenti Vieira Feb 01 '14 at 18:54