Providers are delegated constructors, so they override constructor behavior. However, have you tried just binding the key to the concrete implementation class, instead of a provider wrapper around the class?

pBinder.bind(pInterface).to(pImplementation).in(SINGLETON);

Guice will examine that implementation class, pick out its constructor with @Inject, and then analyze its dependencies. I think the provider wrapper is unnecessary.

Christian.

On 5 Apr 2014, at 10:36, Jochen Wiedmann wrote:

Hi,

one of my modules contains the following code: The Provuder works by
calling Class.newInstance(), which is generallly fine. Except that it
ignores @Inject constructors.

Are there any possibilities to change that?

Thanks,

Jochen

P.S: And, btw: Is the "synchronized" on get really needed?



 private void bind(Binder pBinder, Class<?> pInterface, final
Class<?>pImplementation, String pName) {
     final Class<Object> type;
     if (pInterface == null) {
         type = Generics.cast(pImplementation);
     } else {
         type = Generics.cast(pInterface);
     }
     final Key<Object> key;
     if (Strings.isEmpty(pName)) {
         key = Key.get(type);
     } else {
         key = Key.get(type, Names.named(pName));
     }
     final Provider<Object> provider = new Provider<Object>() {
         private Object instance;

         @Override
         public synchronized Object get() {
             if (instance == null) {
                 try {
                     instance = pImplementation.newInstance();
                 } catch (Throwable t) {
                     throw Exceptions.show(t);
                 }
             }
             return instance;
         }
     };
     pBinder.bind(key).toProvider(provider).in(Scopes.SINGLETON);
 }

--
You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/d/optout.


Christian Gruber :: Google, Inc. :: Java Core Libraries :: Dependency Injection
email: [email protected] :::: mobile: +1 (646) 807-9839

--
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/d/optout.

Reply via email to