It's hard to tell exactly what you are meaning, but I think what you are asking is the IState class is generic to IParser, so both excel handler and word handler both use the same IState implementation but with two different parsers. In this case you're not able to inject the IState into the ContentHandler because Guice doesn't know that the IState for ExcelHandler needs ExcelParser.

I just had a very similar problem and it is the "robot legs" problem mentioned in the FAQ (which you can find example online at the Guice site). What I did is made two separate modules (in your case it would be WordModule and ExcelModule). In each case there's only one way for IState to be constructed so it works. You need to make the modules private so that their IState bindings don't conflict. Then you would expose() only the content handler in each module. When you make the injector you pass both modules. You can pull out the specific content handler via the annotation.

Jason

On 8/22/2010 8:07 AM, gregory wrote:
Newbie Alert .

  My problem is how to use binding annotation in case when chain of
dependency is not defined with the annotation all the way.

Binding annotations *Excel* and *Word* are used to bind
*ContentHandler* and *IParser* interfaces but not *IState* interface
(see *MyModule*). The dependency looks like this:

     ContentHandler ->  IState ->  IParser

     public class ExcelHandler implements ContentHandler {

       @Inject
       public ExcelHandler(@Excel IParser parser) {
           IState = new State(parser);
       }
     }


     public class MyModule extends AbsractModule {

           protected void configure() {
             bind(ContentHandler.class).annotatedWith(Excel.class).
                                        to(ExcelContentHandler.class);
             bind(ContentHandler.class).annotatedWith(Word.class).
                                to(WordContentHandler.class);

             bind(IParser.class).annotatedWith(Excel.class).
                                to(ExcelParser.class);
             bind(IParser.class).annotatedWith(Word.class).
                               to(WordParser.class);

             bind(IState.class).to(State.class);
       }
     }

I obtain initial instance with injector:

     ContentHandler handler =
injector.getInstance(Key.get(ContentHandler.class, Excel.class));

How to use guice to replace explicit constructor?

     IState = new State(parser);


--
You received this message because you are subscribed to the Google Groups 
"google-guice" 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?hl=en.

Reply via email to