On Aug 13, 6:27 pm, zhaoyi <[email protected]> wrote:
> Below is my code. When I running this program, I will get an error
> "Could not find a suitable constructor in java.lang.Integer. Classes
> must have either one (and only one) constructor annotated with @Inject
> or a zero-argument constructor that is not private.". How can I pass a
> parameter to the provider?

Instead of using a provider, write a regular factory class that
provides the requested value. Although you must code the factory by
hand, you can inject values into it. For example,

  public class ImplFactory {
    @Inject Provider<Imple1> imple1Provider;
    @Inject Provider<Imple2> imple2Provider;

    public Interface1 create(int type) {
      return type == 1 ? imple1Provider.get() : imple2Provider.get();
    }
  }

In your client code, simply inject this factory.


--~--~---------~--~----~------------~-------~--~----~
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