Guice does "just in time" binding if it encounters an injection point which requests a type not bound. Guice will look for an Constructor with @Inject annotation or a default constructor in that type. When it encounters a matching constructor it will add "just in time" a binding to the injector.

I mostly try to avoid using just in time bindings because I like the explicity of the bindings in my modules.


On 06/20/2013 04:31 AM, Puneet Lakhina wrote:
HI,

I am trying to migrate some existing code to guice and following is a self contained example of the setup I have:

public class GuiceMailingListQuestion {

    public static interface CountrySpecificArtifact {

        public abstract ISOCountry getCountry();
    }

public interface CountrySpecificArtifactFactory<T extends CountrySpecificArtifact> {
        public T create(ISOCountry country);
    }
    public static abstract class Base<T> {
    }
public static abstract class CountrySpecificBase<T> extends Base<T> implements CountrySpecificArtifact {
        private ISOCountry country;

        @Inject
        public CountrySpecificBase(@Assisted ISOCountry country) {
            this.country = country;
        }
        @Override
        public ISOCountry getCountry() {
            return country;
        }
        public String toString() {
return getClass().getSimpleName() + " country=" + country.getId();
        }
    }
    public static class A extends CountrySpecificBase<String> {
        @Inject
        public A(@Assisted ISOCountry country) {
            super(country);
        }
    }
    public static class B implements CountrySpecificArtifact {
        private ISOCountry country;
        private A baseObject;
        @Inject
        public B(A baseObject, @Assisted ISOCountry country) {
            this.country = country;
            this.baseObject = baseObject;
        }
        @Override
        public ISOCountry getCountry() {
            return country;
        }
        public String toString() {
return "B country=" + country.getId() + " baseObject=" + baseObject.toString();
        }
    }
    public static void main(String[] args) {
        Injector i = Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                install(new FactoryModuleBuilder().build(
new TypeLiteral<CountrySpecificArtifactFactory<A>>() {}));
                install(new FactoryModuleBuilder().build(
new TypeLiteral<CountrySpecificArtifactFactory<B>>() {}));
            }
        });
System.out.println(i.getInstance(Key.get(new TypeLiteral<CountrySpecificArtifactFactory<B>>(){})).create(NFCountry.US)); System.out.println(i.getInstance(Key.get(new TypeLiteral<CountrySpecificArtifactFactory<B>>(){})).create(NFCountry.MX));
    }
}


The part that surprises me about the above example is how does it work at all? When I try to create a B using the CountrySpecificArtiFactory<B> , how does guice figure out how to create an A to pass to B's constructor. All I have told guice is how to create CountrySpecificArtifactFactory<A> not how to create A.

Can you explain why this works at all?

--
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/groups/opt_out.



--
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/groups/opt_out.


Reply via email to