Java Reference
In-Depth Information
property: attachmentDao , contactDao , folderDao , messageDao , and user-
Dao . These match the names that we used in the @Repository annota-
tions. We can also specify the name in the @SpringBean annotation:
@SpringBean("myContactDao")
protected ContactDao contactDao;
In this case, Stripes uses the name myContactDao instead of the name
of the property. Again, the name must match the name that we use
in the @Repository annotation of the corresponding component so that
Stripes can find it, so we'd have to use myContactDao there as well.
Now, BaseActionBean is coded only against the DAO interfaces. Spring
takes care of loading the implementations, and Stripes sets them on
the action bean. If you want to use a different set of implementations,
all you have to do is change the package in applicationContext.xml . You
don't have to change BaseActionBean or recompile any code. In Sec-
tion 12.3 , Testing with Spring and Injected Mock Objects, on page 272 ,
we'll see how we can use this flexibility to load the “real” set of DAO
implementations when running the application and use a set of mocks
when executing automated tests.
Injecting Dependencies in Other Stripes Objects
@SpringBean indicates where to inject a Spring-managed dependency.
Stripes does this automatically for action beans, but not for other
Stripes objects such as the action bean context, type converters, for-
matters, and so on. In the webmail application, we need DAOs in MyAc-
tionBeanContext :
Download email_23/src/stripesbook/ext/MyActionBeanContext.java
public class MyActionBeanContext extends ActionBeanContext {
private FolderDao folderDao = new FolderDaoImpl();
private UserDao userDao = new UserDaoImpl();
}
How do we inject Spring-managed dependencies in there? I could tell
you that we can just refactor MyActionBeanContext so that it doesn't
depend on any DAOs. That would involve moving code between MyAc-
tionBeanContext and BaseActionBean and modifying a few JSPs that cur-
rently obtain information from MyActionBeanContext . Sure, that would
work, but I'd be punting on the issue, wouldn't I? No, let's face the
problem and see how we can use DI on MyActionBeanContext .
We already know that a custom action bean context is a Stripes exten-
sion. The module that Stripes uses to create instances of the action
 
 
 
Search WWH ::




Custom Search