Using DataSources in DAO Classes

We start with a simple implementation of a UserDao interface. We will add more features as we go along, and explain the Spring JDBC classes as we do so. Let us begin with an empty UserDao interface and a JdbcUserDao implementation (see Listing 8-7).

Listing 8-7: UserDao Interface and Implementation

image from book
public interface TestDao { }      public class JdbcTestDao implements TestDao { }
image from book

First we will add a dataSource property to the implementation. The reason we want to add the dataSource property to the implementation class rather than the interface should be quite obvious: the interface does not need to know how the data is going to be retrieved and updated. By adding get/setDataSource methods to the interface, we—in the best-case scenario—force the implementations to declare the getter and setter stubs. Clearly this is not a very good design practice. Take a look at the modified JdbcUserDao class in Listing 8-8.

Listing 8-8: JdbcUserDao with dataSource Property

image from book
public class JdbcTestDao implements TestDao {     private DataSource dataSource;     public void setDataSource(DataSource dataSource) {         this.dataSource = dataSource;     } }
image from book

We can now instruct Spring to configure our userDao bean using the JdbcUserDao implementation and set the dataSource property (see Listing 8-9).

Listing 8-9: Spring Application Context File with dataSource and userDao Beans

image from book
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ¿ "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>          <bean            destroy-method="close">         <!-- omitted for clarity -->     </bean>          <bean  bold">com.apress.prospring.data.jdbc.JdbcTestDao">         <property name="dataSource"><ref local="dataSource"/></property>     </bean>      </beans>
image from book

Spring now creates the testDao bean by instantiating the JdbcTestDao class with the dataSource property set to the dataSource bean.

It is good practice to make sure that all required properties on a bean have been set. The easiest way to do this is to implement the InitializingBean interface and provide an implementation for the afterPropertiesSet() method (see Listing 8-10). This way you make sure that all required properties have been set on your JdbcTestDao. For further discussion of bean initialization, refer to Chapter 5.

Listing 8-10: JdbcTestDao Implementation with InitializingBean

image from book
public class JdbcTestDao implements TestDao, InitializingBean {     private DataSource dataSource;          public void afterPropertiesSet() throws Exception {         if (dataSource == null) {             throw new BeanCreationException("Must set dataSource on UserDao");         }         // more initialization code     }          public void setDataSource(DataSource dataSource) {         this.dataSource = dataSource;     } }
image from book

The code we have looked at so far uses Spring to manage the data source and introduces the TestDao interface and its JDBC implementation. We also set the dataSource property on the JdbcTestDao class in the Spring application context file. Now we expand the code by adding the actual DAO operations to the interface and implementation.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net