Reputation: 37
Which would be the right/better practice when implementing non-jpa/orm DAO layers?
@Repository
public class SampleDao {
private JdbcTemplate jdbcTemplate;
public SampleDao(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
// --- OR ---
public SampleDao(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
Upvotes: 1
Views: 1321
Reputation: 8622
In a Spring Boot application I'd generally recommend injecting JdbcOperations
rather than injecting the JdbcTemplate
or creating a new JdbcTemplate
instance from the DataSource
. The JdbcOperations
interface is implemented by the auto-configured JdbcTemplate
.
The reasons are as follows:
JdbcOperations
doesn't surface any setters so you can't accidentally change things on the shared singleton beanspring.jdbc.template...
application properties to configure itDataSource
if you need to do so for testingUpvotes: 1