Using vraotor 3 in guice

19 views
Skip to first unread message

TL

unread,
Oct 25, 2010, 8:02:33 PM10/25/10
to caelum-vraptor-en
Hi,
I am progressing in the implementation of a guice module, and piece by
piece rebuilding the entire configuration of vraptor in guice. The
problem is that some classes are expected to come from guice, and some
created by vraptor and passed in the constructor. This obviously
cannot work in guice. In constructor injection guice needs to be able
to find all the constructor values.

Example
The filter has:
final RequestInfo request = new RequestInfo(servletContext, chain,
mutableRequest, mutableResponse);

But some classes that come from the injector require this instance in
the constructor:
public ResourceLookupInterceptor(UrlToResourceTranslator translator,
MethodInfo methodInfo,
ResourceNotFoundHandler resourceNotFoundHandler,
MethodNotAllowedHandler methodNotAllowedHandler,
RequestInfo requestInfo) {

How is this expected to work with guice (or any IOC container for that
matter)? If RequestInfo is created in the code, then it cannot be
found by the injector.

There is also overwhelming number of classes that I have to bind with
default values, and I am not really sure that I use the right
defaults.

Is it possible to integrate the two, or should I just give up now?

Lucas Cavalcanti

unread,
Oct 25, 2010, 11:17:25 PM10/25/10
to caelum-v...@googlegroups.com
Hi TL,

Did you start this guice integration from scratch, or from the existing GuiceProvider?

We created this RequestInfo via its constructor because it depends on the current
request and response environment.
Since VRaptor was designed and implemented with Pico and Spring first, this approach
was very helpful.

I know that it is not the best way to work with Guice, and I had to make some workarounds
in order to release the GuiceProvider beta.

There is also overwhelming number of classes that I have to bind with
default values, and I am not really sure that I use the right
defaults.
what classes?

In case you haven't seen the Guice Provider, we have some (little) documentation here:

And the provider implementation here:

If you have better ideas regarding this implementation, please let us know, it would be much appreciated

Thanks
Lucas

TL

unread,
Oct 26, 2010, 2:17:41 AM10/26/10
to caelum-vraptor-en
Here is an example to the classes I have to bind. Some I can just
bind, the others need a provider method because juice requires an
annotation. It would be helpful if there is a guice module that is
part of the integration that has all the binding and factory methods.
Will save me a lot of work, and users can subclass it. As it is now,
it looks like it is not possible because some classes require a
runtime instance like RequestInfo as part of the injection process, at
least the way guice is doing it.

public class VraptorModule extends ServletModule {

@Override
protected void configureServlets() {
bind(EncodingHandler.class).toInstance(new
WebXmlEncodingHandler("UTF-8"));
bind(Container.class).to(GuiceContainer.class).in(Singleton.class);
}

@Provides
@Singleton
InterceptorHandlerFactory createInterceptorHandlerFactory(Container
container){
return new DefaultInterceptorHandlerFactory(container);
}

@Provides
InterceptorStack createInterceptorStack(InterceptorHandlerFactory
factory){
return new DefaultInterceptorStack(factory);
}

@Provides
@RequestScoped
RequestExecution createRequestExecution(InterceptorStack stack){
return new GuiceRequestExecution(stack);
}

@Provides
ResourceLookupInterceptor
craeteResourceLookupInterceptor(UrlToResourceTranslator translator,
MethodInfo methodInfo,
ResourceNotFoundHandler resourceNotFoundHandler,
MethodNotAllowedHandler methodNotAllowedHandler,
RequestInfo requestInfo){
return new ResourceLookupInterceptor(translator, methodInfo,
resourceNotFoundHandler, methodNotAllowedHandler, requestInfo);
}
...



TL

unread,
Oct 26, 2010, 2:24:22 AM10/26/10
to caelum-vraptor-en
Notice the last method for ResourceLookupInterceptor.
It requires RequestInfo, so injection fails. In theory guice can
create that object, (most of it) as request scope.

Lucas Cavalcanti

unread,
Oct 26, 2010, 6:44:40 AM10/26/10
to caelum-v...@googlegroups.com
On VRaptorAbstractModule we are using the Guice 3.0 toConstructor method:
So we can just do:
 bind(EncodingHandler.class).toConstructor(WebXmlEncodingHandler.class.getDeclaredConstructors()[0]);

and we do it automatically via BaseComponents.get(Application|Request)Components()

What is it wrong with VRaptorAbstractModule that you want to change?

[]'s
Lucas

TL

unread,
Oct 26, 2010, 8:43:49 AM10/26/10
to caelum-vraptor-en
I don't use guice 3.0 because it is unreleased yet. Otherwise I will
give it a try.

Lucas Cavalcanti

unread,
Oct 26, 2010, 1:27:41 PM10/26/10
to caelum-v...@googlegroups.com
So you have to either bind vraptor components manually, or annotate all VRaptor components constructors with @Inject.

you can get the right values on the BaseComponents class.
Reply all
Reply to author
Forward
0 new messages