File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Creating SimpleJdbcTemplate instance of SimpleJdbcDaoSupport class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Creating SimpleJdbcTemplate instance of SimpleJdbcDaoSupport class" Watch "Creating SimpleJdbcTemplate instance of SimpleJdbcDaoSupport class" New topic
Author

Creating SimpleJdbcTemplate instance of SimpleJdbcDaoSupport class

Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
While i study spring from spring in action, it mentioned that we need to extend SimpleJdbcDaoSupport so we can directly use getSimpleJdbcTemplate() of SimpleJdbcDaoSupport for our DAO class so that we can reduce the boilerplate code for wire the SimpleJdbcTemplate for each DAO.

Below is a section i copy from spring in action 3 book, (page 132)


When JdbcSpitterDao has its dataSource property configured, it’ll internally create a SimpleJdbcTemplate instance for you. This eliminates the need to explicitly declare a SimpleJdbcTemplate bean in Spring.

<bean id="spitterDao"
class="com.habuma.spitter.persistence.JdbcSpitterDao">
<propertyname="dataSource"ref="dataSource"/>
</bean>


But i dont understand how come if we set the DAO the dataSource it will internally create a SimpleJdbcTemplate instance. Also i dont see a dataSource property in JdbcSpitterDao class they mentioned.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

Actually don't use SimpleJdbcTemplate at all. It has been deprecated since Spring 3.1 in favor of JdbcTemplate and NamedParameterJdbcTemplate. The JdbcTemplate and NamedParameterJdbcTemplate now provide all the functionality of the SimpleJdbcTemplate.

Here is an example of how it should look.







[How To Ask Questions][Read before you PM me]
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
Bill Gorder wrote:The JdbcTemplate and NamedParameterJdbcTemplate now provide all the functionality of the SimpleJdbcTemplate.


When using JdbcTemplate also we need to extend the JdbcDaoSupport in order to get the jdbc template by calling getJdbcTemplate() instead of wiring it in DAO.

So the problem is there is datasource property in JdbcDaoSupport class is final know. But lets say we call the super.setDataSource in DAO. But is it possible if we just set that datasource property in the DAO and it will return a instance of JdbcDaoSupport as the author mentioned for SimpleJdbcTemplate.

What your code does make scenes where first we set the data source to template bean and wire the template bean with DAO.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

When using JdbcTemplate also we need to extend the JdbcDaoSupport in order to get the jdbc template by calling getJdbcTemplate() instead of wiring it in DAO.


My first question is going to be why? JdbcTemplate is thread safe. I prefer to initialize it once in my Spring configuration and provide it where needed. Another approach you see sometimes is this



but once again I don't see the use of passing in the datasource. I just initialize the JdbcTemplate once in configuration and re-use it for all my DAO's like shown in the first example.

Now as to your other question.
Why do you need to extend JdbcDaoSupport? If there is a need fine but otherwise I feel you are extending a framework class and needlessly coupling your application to the framework. In that case I prefer to use the approach above.

JdbcDaoSupport is a wrapper class. It wraps the JdbcTemplate class. So yes you can simply inject the datasource into the dao in this case the JdbcTemplate will be initialized automatically.

for example:



Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

Harshana Dias wrote:
So the problem is there is datasource property in JdbcDaoSupport class is final know. .


No, as a matter of fact the only instance variable in JdbcDaoSupport is

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating SimpleJdbcTemplate instance of SimpleJdbcDaoSupport class