Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are template classes in Spring Java? Why are they called templates? For example jdbc-template, jms-template etc

Tags:

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.

like image 634
Horse Voice Avatar asked Jan 31 '14 20:01

Horse Voice


People also ask

What are templates in spring?

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.

What is JdbcTemplate Java?

JdbcTemplate class is the central class in the JDBC core package. It simplifies the use of JDBC and helps to avoid common errors. It executes core JDBC workflow, leaving the application code to provide SQL and extract results.

What are templates in spring boot?

Spring Boot starters are templates that contain a collection of all the relevant transitive dependencies that are needed to start a particular functionality. Each starter has a special file, which contains the list of all the provided dependencies Spring provides. These files can be found inside pom.

What is spring boot JdbcTemplate?

Spring JdbcTemplate is a powerful mechanism to connect to the database and execute SQL queries. It internally uses JDBC api, but eliminates a lot of problems of JDBC API.


2 Answers

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.

like image 154
Koitoer Avatar answered Oct 05 '22 17:10

Koitoer


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.

like image 30
Angular University Avatar answered Oct 05 '22 18:10

Angular University