My plan was to let the user specify the int at runtime and then pass
it to the module like:


public class Test {

  public static void main(String[] args) {
    // Get userinput. Path to xml frame.
    int userInt = Integer.parseInt(args[0]);
    Injector injector = Guice.createInjector(new MyCarModule
(userInt));
    CarShop carShop = injector.getInstance(CarShop.class);
    }
}

Then in the module I can do:

public class MyCarModule extends AbstractModule {
  private int tt;
  public MyCarModule(int tt) {
    this.tt = tt;
  }

  @Override
  protected void configure() {

    bind(Automobile.class).to(Ford.class);
    bind(String.class).annotatedWith(Names.named
("LicenseKey")).toInstance(tt);



But is it this how guice is intented to be used (passing external
resources to the module constructor)?


On Jan 14, 11:37 pm, Fred Faber <[email protected]> wrote:
> Where is the value of the int coming from?
>
> If it's known at configure() time, then simply bind it:
>
> bind(int.class).toInstance(60);
>
> most likely you'll need to annotate it:
>
> class Ford implements Automobile {
>
>     @Retention(RetentionPolicy.RUNTIME)
>     @Target(ElementType.PARAMETER)
>     @BindingAnnotation
>     @interface  ForFord { }
>
>     �...@inject
>      Ford(@ForFor int maxSpeed) {
>        ...
>      }
>   }
>
> at which point you can use the type converters in guice to do:
>
> bindConstant().annotatedWith(ForFord.class).to(65);
>
> -Fred
>
> On Thu, Jan 14, 2010 at 4:56 PM, motes <[email protected]> wrote:
> > I have this simple class:
>
> > public class Ford implements Automobile {
> >        private int maxSpeed;
>
> >        public Ford(int speed) {
> >          System.out.println("Driving a Ford!");
> >                this.maxSpeed = speed;
> >        }
>
> >       �...@override
> >        public int getMaxSpeed() {
> >                return maxSpeed;
> >        }
>
> >        public void printBrand(){
> >          System.out.println("Ford");
>
> >        }
> > }
>
> > I would like to inject this implementation into the CarShop below:
>
> > public class CarShop {
> >  private Automobile car;
> >       �...@inject
> >        public CarShop(Automobile car) {
> >          this.car = car;
> >        }
>
> >        public void printBrand(){
> >          car.printBrand();
> >        }
>
> >        public void printMaxSpeed(){
> >          car.getMaxSpeed();
> >        }
> > }
>
> > In my Module I do:
>
> > public class MyCarModule extends AbstractModule {
> >       �...@override
> >        protected void configure() {
> >            bind(Automobile.class).to(Ford.class);
> >        }
> > }
>
> > But how do I get the int passed to the constructor in my Ford
> > implementation?
>
> > I have looked at:
>
> >http://code.google.com/docreader/#p=google-guice&s=google-guice&t=Fre...
>
> > but it seems pretty overkill to create a factory or an AssistedInject
> > to pass an int to a constructor.
>
> > Any ideas?
>
> > --
> > 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]<google-guice%[email protected]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/google-guice?hl=en.
-- 
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