Hi,
I would love to share my implementation, but its does not work :)
One way to implement it is that the guice web container is
instantiating the juice filter since it can already create filters.
This way guice can inject HttpServletRequest and response and a few
other things to any component that needs it. It is possible to just
create
Another way is that vraptor is starting as a normal filter (web.xml),
and then gets the Injector from a context attribute. This is how I
implemented it. If vraptor needs to add more components to the IOC
container, it can wrap it in another injector that contains the extra
vraptor implementation classes, and then use that injector.
In the current implementation, it would be useful to have a method
where I can specify my own modules, so if I have my DAOs and services
already in modules, I can pass them in.
Another thing to look at is the FreeMarker servlet. It is very easy to
extend by subclassing.
Another idea is that there is a clear separation between injected
classes and request data. So for example, RequestInfo is a data class,
and it is never part of an injection chain. It is passed as a runtime
argument to method calls. Other classes that are injected, never have
this object as a required injection value.
My current implementation looks something like that:
public class GuiceContainerProvider implements ContainerProvider {
static final LifecycleScope REQUEST = new RequestCustomScope();
Container container;
@Override
public <T> T provideForRequest(RequestInfo request,
Execution<T> execution) {
VRaptorRequestHolder.setRequestForCurrentThread(request);
REQUEST.start();
try {
return execution.insideRequest(container);
} finally {
REQUEST.stop();
VRaptorRequestHolder.resetRequestForCurrentThread();
}
}
@Override
public void start(ServletContext context) {
final Injector injector = (Injector) context
.getAttribute(Injector.class.getName());
container = injector.getInstance(Container.class);
}
@Override
public void stop() {
container = null;
}
}
public class GuiceContainer implements Container {
private final Injector injector;
@Inject
private GuiceContainer(Injector injector) {
this.injector = injector;
}
@Override
public <T> T instanceFor(Class<T> type) {
return injector.getInstance(type);
}
@Override
public <T> boolean canProvide(Class<T> type) {
// TODO: The factory method will create a new object every time, not
necessarily good
// maybe just return true.
return instanceFor(type) != null;
}
}