Hello,
I'm using Guice 4.0 beta 2. I have some experience with Guice 3.0 on a previous project, but no experience with custom scopes.
In my new application I have a Provider for an ApplicationConfiguration object. The Provider selects an ApplicationConfiguration object from the servlet context, based on information in the HttpServletRequest which is injected into the Provider.
The binding looks like this:
bind(ApplicationConfiguration.class).toProvider(ApplicationConfigurationProvider.class).in(RequestScoped.class);
So far so good. The Provider correctly provides the ApplicationConfiguration for each request.
Note that the ApplicationConfiguration is injected into other Provider objects.
Now, I need to do some processing outside of a servlet request that still depends on the ApplicationConfiguration object. Specifically, I need to do some processing that is started from a servlet context listener. The processing occurs after Guice has been initialized. I have access to the correct ApplicationConfiguration instance and don't need the Provider to provide one.
I created a custom scope following the instructions on the "Custom Scopes" wiki page, and registered the scope and its annotation with Guice.
Since I already have the correct ApplicationConfiguration instance and don't need a Provider to provide one, I seed the scope with the ApplicationConfiguration instance. I then try to do something with the Injector:
SimpleScope scope = ....;
scope.enter();
try
{
scope.seed(Key.get(ApplicationConfiguration.class), appConfig);
//ask the injector for an instance of something....
}
finally
{
scope.exit();
}
It's failing when I ask the injector for something that depends on an ApplicationConfiguration. Here's part of the error:
1) Error in custom provider, com.google.inject.OutOfScopeException: Cannot access scoped object. Either we are not currently inside an HTTP Servlet request, or you may have forgotten to apply com.google.inject.servlet.GuiceFilter as a servlet filter for this request.
Now, I think it might be failing on another Provider that is also request scoped, and this Provider has an ApplicationConfiguration injected into it, but I'm not sure:
bind(EntityManager.class).toProvider(EntityManagerProvider.class).in(RequestScoped.class);
(The EntityManager is picked based on the ApplicationConfiguration)
So, I thought I could scope another Provider that is not in request scope, but Guice wouldn't let me do that.
Does anybody have any thoughts on what is going wrong? Do I need more or additional bindings? Am I not seeding the custom scope correctly?
Thank you!!!
-Ryan