Status: New
Owner: ----

New issue 645 by [email protected]: Scoping issue when using CheckedProvider
http://code.google.com/p/google-guice/issues/detail?id=645

I have a class to be injected, which may throw exception from its
constructor. As recommend in Guice wiki, I need to use CheckedProvider
in order to catch and handle that exception. I have also tried to add some scoping requirement on my class either Singleton or RequestScoped. But it seems to me that the scoping requirement is not being honored.

Attached is the eclipse project of my test program.

I used a simple hack to trigger the exception, as in GrapeImpl's constructor.

First, if I disable the exception throwing in the constructor. The result is:
---------------------------
Jul 29, 2011 11:03:56 AM com.tsa.test.GrapeImpl info
INFO: com.tsa.test.GrapeImpl@15cda3f                 <-- object 1
Jul 29, 2011 11:03:56 AM com.tsa.test.GrapeImpl info
INFO: GrapeImpl.info: 0
Jul 29, 2011 11:03:56 AM com.tsa.test.GrapeImpl info
INFO: com.tsa.test.GrapeImpl@df8f5e                  <-- object 2
Jul 29, 2011 11:03:56 AM com.tsa.test.GrapeImpl info
INFO: GrapeImpl.info: 1
-------------------------------------
Although I bind it as Singleton, it seems two different objects are created.

If I enable the exception throwing, the result:
----------------------
Jul 29, 2011 11:11:20 AM com.tsa.test.GrapeImpl info
INFO: com.tsa.test.GrapeImpl@15cda3f
Jul 29, 2011 11:11:20 AM com.tsa.test.GrapeImpl info
INFO: GrapeImpl.info: 0
Jul 29, 2011 11:11:20 AM com.tsa.test.GBox info
WARNING: Failed to get a grape!
------------------------------
The constructor is called twice (which is wrong, as it is bound as Singleton!) and the second time throws an exception.

So I am wondering what the comment at "http://code.google.com/p/google-guice/wiki/ThrowingProviders#Notes_on_Scoping"; really means?

A little suggestion on Guice documentation (based on my own experience while picking up Guice during the last few weeks). The code samples in the wiki page are all partial ones, scattered around at different places, just to illustrate the point. Unfortunately, there's no complete samples for newbies to run, to try out, so to understand. It would be of great help if there are complete sample programs demonstrating different functionalities of Guice.

Attachments:
        guice-test.zip  9.7 KB

--
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice-dev?hl=en.

Reply via email to