Skip to content
Advertisement

using Spring JdbcTemplate – injecting datasource vs jdbcTemplate

As per Spring documentation, the steps to use Spring JdbcTemplate is as follows:

JavaScript

And then,

JavaScript

Basically, the JdbcTemplate is created inside the Component class using the setter for datasource.

Is there anything wrong with doing it this way instead so that there is exactly ONE instance of jdbcTemplate in the application?

JavaScript

And then injecting the jdbcTemplate itself directly into the Component

JavaScript

Is there a reason why the jdbcTemplate itself must not be injected into the component class directly?

SGB

Advertisement

Answer

You can do what you want. The javadoc of JdbcTemplate even clearly says it:

Can be used within a service implementation via direct instantiation with a DataSource reference, or get prepared in an application context and given to services as bean reference.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement