On 27 Oct 2011, at 06:14, Jeff wrote:

> I want to do something like:
> @Inject @Named("conf")
> Properties config;
>  
> @Inject @Named("lang")
> Properties language;
> Where these objects are singletons initialized once at runtime with 
> environment-specific information.
>  
> I've tried various things and none result in my Provider or @Provides methods 
> getting called.  Here is a trimmed down version of my code using a Provider 
> class:
>  
> MyModule.java:
> public class SeleniumInjectionModule extends AbstractModule {
>   @Override
>   protected void configure() {
>     
> bind(Properties.class).annotatedWith(Names.named("conf")).toProvider(ConfigProvider.class);
>     
> bind(Properties.class).annotatedWith(Names.named("lang")).toProvider(LanguageProvider.class);
>   }
> }
> ConfigProvider.java:
> @Singleton
> public class ConfigProvider implements Provider<Properties> {
>   private final Properties config = new Properties();
>  
>   public ConfigProvider() {
>     //Load properties file based on 'config' system property value set in 
> Maven profile
>     ...
>     config.load(...);
>     ...
>   }
>   @Override
>   public Properties get() {
>     return config;
>   }
> }
>  
> LanguageProvider.java:
> @Singleton
> public class LanguageProvider implements Provider<Properties> {
>   private final Properties lang = new Properties();
>  
>   public LanguageProvider() {
>     //Load properties file based on 'language' system property value set in 
> Maven profile
>     ...
>     lang.load(...);
>     ...
>   }
>   @Override
>   public Properties get() {
>     return lang;
>   }
> }
>  In my Global Init code:
>     Guice.createInjector(new MyModule());
>  
> What am I missing? 

Where's the class containing the injected config/language fields mentioned in 
the start of this email?

Assuming that class is called Foo then you need to use 
injector.getInstance(Foo.class) - or injector.injectMembers(myFoo) if you 
already have an instance of Foo - to start the injection process and inject 
those fields. Note that you don't need to do this for everything, just the 
class at the beginning of the injection graph to kick things off.

> --
> Jeff Vincent
> [email protected]
> See my LinkedIn profile at:
> http://www.linkedin.com/in/rjeffreyvincent
> I ♥ DropBox !! 
>  
> 
> -- 
> 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.

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