@Inject @Named("conf")Properties config;@Inject @Named("lang")Properties language;
public class SeleniumInjectionModule extends AbstractModule {@Overrideprotected void configure() {bind(Properties.class).annotatedWith(Names.named("conf")).toProvider(ConfigProvider.class);bind(Properties.class).annotatedWith(Names.named("lang")).toProvider(LanguageProvider.class);}}
@Singletonpublic 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(...);...}@Overridepublic Properties get() {return config;}}
@Singletonpublic 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(...);...}@Overridepublic Properties get() {return lang;}}
--Jeff VincentSee my LinkedIn profile at: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 google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
(guiceModule = MyModule.class) annotation, but I wanted to avoid the need to put this on EVERY test class and thought I could insert my Modules/Providers and enable my custom bindings to work globally.The classes containing the injected fields are TestNG test classes. I am creating the Injector using the ServiceLoader mechanism within a class that implements org.testng.ISuiteListener. TestNG automatically picks up and runs the onStart() method before doing anything else and I call createInjector() there.Though, I think I just realized my problem (besides trying to learn/use Guice from within another framework I'm also just learning).The key bit I had not grasped previously was that for Guice/@Inject to work, object instantiation must be done directly by Guice (Injector.getInstance()), at least for the root node of the object graph (I think).Since TestNG instantiates classes without Guice by default, it was just creating normal instances and the @Inject annotations weren't getting processed.TestNG does support Guice via @Test(guiceModule = MyModule.class)annotation, but I wanted to avoid the need to put this on EVERY test class and thought I could insert my Modules/Providers and enable my custom bindings to work globally.I'm still playing with my code, but I feel like I'm on the right track. Can anyone confirm or help me understand if there is a different way?
@Provides @Named("ENV") @Singletonpublic Properties EnvironmentProvider() {//setup envreturn env;}@Provides @Named("LANG") @Singleton @Injectpublic Properties LanguageProvider(@Named("ENV") Properties env) {//load language files based on lang setting in envreturn lang;}
Thanks for the great info and helping me work things out. The fog is clearing.Since my language provider depends on the environment, is it kosher to do something like:@Provides @Named("ENV") @Singletonpublic Properties EnvironmentProvider() {//setup envreturn env;}@Provides @Named("LANG") @Singleton @Inject
public Properties LanguageProvider(@Named("ENV") Properties env) {//load language files based on lang setting in envreturn lang;}And if I mark these as @Singleton, am I the one that needs to store and manage the instance or does Guice cache the object returned for lang and env and never call this method directly again?
public final Object newInstance(@SuppressWarnings("unchecked") final Constructor constructor, final Object... args) {
ObjectFactoryImpl impl = new ObjectFactoryImpl();
Object testInstance = impl.newInstance(constructor, args);
Injector injector = Guice.createInjector(this.overrideAbstractMethodToReturnListOfModules());
injector.injectMembers(testInstance);
}
Is the @Singleton scope based on the instance of the Injector being used?I have two classes that aren't getting injected. One is a static factory class (ThreadResourceFactory). The other is a class that the factory instantiates for me (ThreadResource).In my TestNG @BeforeClass method, I call ThreadResourceFactory.get() to retrieve an instance of ThreadResource for the current TestNG test thread.Since I couldn't figure out a way to retrieve the injector previously created by TestNG, I did the following in the ThreadResource constructor:Guice.createInjector(new MyModule()).injectMembers(this);This works sort of, but because I create another injector, it seems to be managing another set of @Singletons for that new Injector instance.
I'm sure there is a better way to do this, I am just unsure how. Any suggestions?