Java Reference
In-Depth Information
public
Resolution intercept(ExecutionContext context)
throws
Exception
{
injector.injectMembers(context.getActionBeanContext());
Resolution resolution = context.proceed();
injector.injectMembers(context.getActionBean());
return
resolution;
}
}
That's all there is to it! Well, almost. The
injector
we see there is a Guice
Injector
object, which is configured with one or more Guice
Module
s. In
a nutshell, a
Module
is a Java class that tells Guice which implemen-
tations to use for which interfaces. Then, dependencies are injected by
annotating properties with Guice's
@Inject
annotation.
Here's how we create a
Module
that wires up our DAOs:
package
stripesbook.ext.guice.config;
public class
GuiceConfigModule
extends
AbstractModule {
@Override
protected void
configure() {
bind(AttachmentDao.
class
).to(AttachmentDaoImpl.
class
);
bind(ContactDao.
class
).to(ContactDaoImpl.
class
);
bind(FolderDao.
class
).to(FolderDaoImpl.
class
);
bind(MessageDao.
class
).to(MessageDaoImpl.
class
);
bind(UserDao.
class
).to(UserDaoImpl.
class
);
}
}
That's pretty straightforward. Next, we have to get our interceptor to
load up this
Module
and use it to create an
Injector
. A simple solution is
to piggyback onto the Stripes extensions mechanism: drop our
Module
into an extension package, and have our interceptor automatically find
it. Using the
Configuration
that Stripes passes to the
init
( ) method, we
can call
getBootstrapPropertyResolver
( ) and from there use one of several
utility methods that returns the classes that are compatible with the
class or interface that we specify:
package
stripesbook.ext.guice.interceptor;
@Intercepts(LifecycleStage.ActionBeanResolution)
public class
GuiceInterceptor
implements
Interceptor, ConfigurableComponent
{
public static final
String MODULES = "Guice.Modules";
Search WWH ::
Custom Search