Java Reference
In-Depth Information
Other Resources
Stripersist makes it easy to use a JPA implementation. If JPA is al-
together not for you, using another solution to communicate data
between your Java model and a database—be it iBATIS ( http://www.
ibatis.org ) , Cayenne ( http://cayenne.apache.org ) , or even plain JDBC—
wouldn't be difficult with Stripes. Using a DAO layer is all about hiding
the implementation details from the client code. You would write your
DAOs to use iBATIS, and Stripes would call the DAOs without being
affected by what framework is used to do the work. The only difference
is that you wouldn't benefit from the transaction support, type conver-
sion, and formatting that Stripersist provides; you'd have to implement
that yourself.
12.2
Dependency Injection with Spring
When you have a class that depends on the services of another class,
such as an action bean needing a DAO, you can just create an instance
of the dependency with the new operator. This approach is simple and
easy to follow; you see which class is being used to satisfy the depen-
dency directly in the code. We've been doing this so far. For example,
BaseActionBean contains the implementations of the DAOs:
Download email_23/src/stripesbook/action/BaseActionBean.java
public abstract class BaseActionBean implements ActionBean {
protected AttachmentDao attachmentDao = new AttachmentDaoImpl();
protected ContactDao contactDao = new ContactDaoImpl();
protected FolderDao folderDao = new FolderDaoImpl();
protected MessageDao messageDao = new MessageDaoImpl();
protected UserDao userDao = new UserDaoImpl();
}
Although this approach is simple and straightforward, it is also limited.
We're using DAO interfaces so that we can easily swap implementations
without affecting the calling code. But using a different implementation
means hunting down the places where we're creating new instances,
replacing them with the alternative implementation and recompiling.
What we gain in simplicity, we lose in flexibility (which is how these
things often go).
Dependency injection is the concept of providing, from the outside, im-
plementations to classes that need them. This way, classes have ref-
erences only to interfaces, and not to any specific implementation.
Using this technique, BaseActionBean would have references only to
 
 
 
Search WWH ::




Custom Search