2009/10/17 eric <[email protected]> > > Thanks for your replay, Stuart. > I learned that bindConstant() could do, but my problem is I need to > pass various strings, which might comes from the user input,
if it's fixed at startup then you can feed the values into a module to configure the bindings if the input changes over the life of the application then you probably need assisted inject: http://code.google.com/p/google-guice/wiki/AssistedInject which lets you combine Guice injected parameters with normal (runtime) parameters to StringBinding. Is there any better solution than calling > Guice.createInjector().getInstance()? > not sure what you mean here - usually you call getInstance (or something similar) on the main application type to bootstrap the injection, other dependencies are filled in by the injector automatically according to @Inject annotations On Oct 15, 11:39 pm, Stuart McCulloch <[email protected]> wrote: > > 2009/10/14 eric <[email protected]> > > > > > > > > > > > > > I have a super basic problem: > > > > > public class StringBinding { > > > private String name; > > > > > @Inject > > > public StringBinding(String name){ > > > this.name = name; > > > } > > > > > public void hi(){ > > > System.out.println(this.name); > > > } > > > } > > > > > How can I let hi() print the name I specified with Guice? > > > > you mean like this...? > > > > public class StringBinding { > > private String name; > > > > @Inject > > public StringBinding(@Named("person") String name) { > > this.name = name; > > } > > > > public void hi() { > > System.out.println(this.name); > > } > > > > public static void main(String[] args) { > > Guice.createInjector(new AbstractModule() { > > protected void configure() { > > > > bindConstant().annotatedWith(Names.named("person")).to("Barney"); > > } > > }).getInstance(StringBinding.class).hi(); > > } > > } > > > > you can also use "Names.bindProperties(binder, properties)" to bind a > whole > > load of constants at once > > > > > > > > -- > > Cheers, Stuart > > > -- Cheers, Stuart --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
