xiaodududu / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Scoping issue when using CheckedProvider #645

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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. 

Original issue reported on code.google.com by laola...@gmail.com on 29 Jul 2011 at 3:17

Attachments:

GoogleCodeExporter commented 9 years ago
Hi,

Found the problem. The issue can be closed.

It lies in the injection part of the CheckedProvider. 

public class GrapeImplProvider implements FruitProvider<Grape> {

  @Override
  public Grape get() throws GrapeException {
    return new GrapeImpl();
  }

}

Originally, I inject the implementation class:
-----------------------------------------------
private final GrapeImplProvider grapeProvider;

  @Inject
  public GBox(GrapeImplProvider grapeProvider) {
    this.grapeProvider = grapeProvider;
  }
-------------------------------------------------

The correct way: should inject the interface:
-------------------------------------------------
private final FruitProvider<Grape> grapeProvider;

  @Inject
  public GBox(FruitProvider<Grape> grapeProvider) {
    this.grapeProvider = grapeProvider;
  }

If injecting interface, the scope requirement works well.

Original comment by laola...@gmail.com on 29 Jul 2011 at 9:21

GoogleCodeExporter commented 9 years ago
I recommend using the @CheckedProvides annotation on a method in the Module, 
which will make writing checked providers a lot simpler (no need for the 
GrapeImplProvider class).

Original comment by sberlin on 29 Jul 2011 at 2:16

GoogleCodeExporter commented 9 years ago
Thanks for the help. Changed to CheckedProvides

Original comment by laola...@gmail.com on 29 Jul 2011 at 3:10