Java Reference
In-Depth Information
bean context is also a Stripes extension, called the action bean context
factory. We can easily extend the Stripes default implementation, obtain
the action bean context object that it creates, and inject dependen-
cies before returning it. Stripes provides a convenient
SpringHelper.inject-
Beans
( )
method
to
detect
annotations
and
then
inject
@SpringBean
dependencies:
package
stripesbook.ext;
public class
MyActionBeanContextFactory
extends
DefaultActionBeanContextFactory
{
@Override
public
ActionBeanContext getContextInstance(
HttpServletRequest req, HttpServletResponse resp)
throws
ServletException
{
ActionBeanContext actionBeanContext
=
super
.getContextInstance(req, resp);
ServletContext servletContext =
StripesFilter.getConfiguration().getServletContext();
SpringHelper.injectBeans(actionBeanContext, servletContext);
return
actionBeanContext;
}
}
@SpringBean
now works in
MyActionBeanContext
:
public class
MyActionBeanContext
extends
ActionBeanContext {
@SpringBean
private
FolderDao folderDao;
@SpringBean
private
UserDao userDao;
}
We can also use this technique to inject Spring dependencies into other
non-action-bean Stripes objects—type converters, formatters, and so
on. Subclass the Stripes default factory, call the superclass method to
create the object, and use
SpringHelper.injectBeans
( ) on the object before
returning it. Besides a
ServletContext
object,
injectBeans
( ) also accepts
an
ActionBeanContext
or a Spring
ApplicationContext
to be able to load
the components from Spring.
Search WWH ::
Custom Search