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=FrequentlyAskedQuestions
>
>
> 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