Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Spring JdbcTemplate - injecting datasource vs jdbcTemplate

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

    <?xml version="1.0" encoding="UTF-8"?>     <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns:context="http://www.springframework.org/schema/context"         xsi:schemaLocation="             http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd             http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-3.0.xsd">          <!-- Scans within the base package of the application for @Components to configure as beans -->         <context:component-scan base-package="org.springframework.docs.test" />          <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">             <property name="driverClassName" value="${jdbc.driverClassName}"/>             <property name="url" value="${jdbc.url}"/>             <property name="username" value="${jdbc.username}"/>             <property name="password" value="${jdbc.password}"/>         </bean>          <context:property-placeholder location="jdbc.properties"/>      </beans> 

And then,

    @Repository     public class JdbcCorporateEventDao implements CorporateEventDao {          private JdbcTemplate jdbcTemplate;          @Autowired         public void setDataSource(DataSource dataSource) {             this.jdbcTemplate = new JdbcTemplate(dataSource);         }          // JDBC-backed implementations of the methods on the CorporateEventDao follow...     } 

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?

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"     p:dataSource-ref="dataSource"  /> 

And then injecting the jdbcTemplate itself directly into the Component

@Repository public class JdbcCorporateEventDao implements CorporateEventDao {     @Resource("jdbcTemplate")     private JdbcTemplate jdbcTemplate;       // JDBC-backed implementations of the methods on the CorporateEventDao follow... } 

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

SGB

like image 225
SGB Avatar asked Jul 25 '13 20:07

SGB


People also ask

What is the difference between JdbcTemplate and HibernateTemplate?

HibernateTemplate v/s JdbcTemplate Spring provides support for both hibernate and JDBC template classes. It provides template classes which contains all common code. But JDBC as we all know is not an ORM tool it does not represent rows as objects whereas Hibernate does that.

What is the difference between JdbcTemplate and NamedParameterJdbcTemplate?

Functionally, there's no difference between Spring's JdbcTemplate and it's variant, NamedParameterJdbcTemplate except for : NamedParameterJdbcTemplate provides a better approach for assigning sql dynamic parameters instead of using multiple '?' in the statement.

Does JdbcTemplate provide DataSource access?

The JdbcTemplate can be used within a DAO implementation through direct instantiation with a DataSource reference, or be configured in a Spring IoC container and given to DAOs as a bean reference. The DataSource should always be configured as a bean in the Spring IoC container.

What are the benefit of Spring JdbcTemplate over the JDBC API?

Advantage of using SpringJDBC over Traditional JDBC API JdbcTemplate provides a great exception handling mechanism that is more specific to deal with the database, it converts the standard JDBC SQLExceptions into RuntimeExceptions which are generic and more informative, allowing developers to better identify the error.


2 Answers

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.

like image 71
JB Nizet Avatar answered Sep 28 '22 18:09

JB Nizet


In the spring-context.xml add the following and

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">     <property name="dataSource" ref="dataSource"/> </bean> 

and directly you can use jdbcTemplate by autowiring like

  @Autowired JdbcTemplate jdbcTemplate; 

example:

this.jdbcTemplate.query("select * from ******",new RowMapper()); 
like image 38
Odaiah Avatar answered Sep 28 '22 18:09

Odaiah