I want to use google guice to make properties available in all classes of my application. I defined a Module which loads and binds the properties file Test.properties.
Property1=TEST
Property2=25
package com.test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
public class TestConfiguration extends AbstractModule {
#Override
protected void configure() {
Properties properties = new Properties();
try {
properties.load(new FileReader("Test.properties"));
Names.bindProperties(binder(), properties);
} catch (FileNotFoundException e) {
System.out.println("The configuration file Test.properties can not be found");
} catch (IOException e) {
System.out.println("I/O Exception during loading configuration");
}
}
}
I'm using a main class where I create a injector to inject the properties.
package com.test;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class Test {
public static void main(String[] args) {
TestConfiguration config = new TestConfiguration();
Injector injector = Guice.createInjector(config);
TestImpl test = injector.getInstance(TestImpl.class);
}
}
package com.test;
import com.google.inject.Inject;
import com.google.inject.name.Named;
public class TestImpl {
private final String property1;
private final Integer property2;
#Inject
public TestImpl(#Named("Property1") String property1, #Named("Property2") Integer property2) {
System.out.println("Hello World");
this.property1 = property1;
this.property2 = property2;
System.out.println(property1);
System.out.println(property2);
}
}
Now my question. If my TestImpl creates other classes where I also need to inject properties, and those classes also need to inject properties what is the correct way to do this?
Pass the injector to all subclasses and then use injector.getInstance(...) to create the subclasses?
Instanciate a new injector like
TestConfiguration config = new TestConfiguration();
Injector injector = Guice.createInjector(config);
TestImpl test = injector.getInstance(TestImpl.class);
in all nested classes?
Is there an other approach to make the properties available in all classes?
Pass the injector to all subclasses and then use
injector.getInstance(...) to create the subclasses?
no, by doing this you are defeating the purpose of the dependency injection pattern and also coupling all your implementation to Guice. Your implementations should not interact at all with guice, except through the (now standardized) annotations.
Instanciate a new injector like
TestConfiguration config = new TestConfiguration();
Injector injector = Guice.createInjector(config);
TestImpl test = injector.getInstance(TestImpl.class);
in all nested classes?
no, and this is even worse cause you will end up with multiple injectors, hence multiple contexts which will prevent a proper usage of the scopes.
Ideally, you should only use the injector during the bootstrapping of your application. Of course the way to bootstrap it will largely depend of the application.
Is there an other approach to make the properties available in all
classes?
The properties could be injected the same way you did for TestImpl.
If you want TestImpl to use let say a service which also needs some properties (or other services), just let Guice inject it to TestImpl. Guice is taking care of all the instantiation/wiring. You should only tell Guice "how to proceed", by using the binder, when Guice cannot figure this out itself :
public class TestImpl {
private final String property1;
private final Integer property2;
private final IService service;
#Inject
public TestImpl(#Named("Property1") String property1, #Named("Property2") Integer property2, IService service) {
this.property1 = property1;
this.property2 = property2;
this.service= service;
}
}
}
Library "Governator" provide a configuration mapping feature for guice injection. The approach is different, but load from properties files is available.
https://github.com/Netflix/governator/wiki/Configuration-Mapping
The library Guice configuration can inject for you values from Properties or JSON files to your services.
You can inject from the file application.properties to your service as :
#BindConfig(value = "application", syntax = PROPERTIES)
public class Service {
#InjectConfig
private int port;
#InjectConfig
private String url;
#InjectConfig
private Optional<Integer> timeout;
}
You must simply install the modules ConfigurationModule
public class GuiceModule extends AbstractModule {
#Override
protected void configure() {
install(ConfigurationModule.create());
requestInjection(Service.class);
}
}
guice-config
Usage example:
At first we have a properties file 'config.properties':
test.key=test value
And we want to inject this value like this:
#Inject
#Config( Property.TEST_KEY )
private String injectedValue;
We need to load contents of file 'config.properties' into java.util.Properties and pass it to Config module:
Properties props = new Properties();
props.load(...);
Module configModule = new ConfigModule( props, Property.values() );
... and injecting:
Injector injector = Guice.createInjector( configModule );
TestClass testClass = injector.getInstance( TestClass.class );
String injectedValue = testClass.getInjectedValue();
injected value will be 'test value'...
"Now my question. If my TestImpl creates other classes where I also need to inject properties, and those classes also need to inject properties what is the correct way to do this?"
Rule of thumb: avoid the use of "new" Unless it is absolutely necessary, dont let your Impl Class "create other classes". Instead, tell your TestImpl that when created with guice, it should get the required instances injected.
Related
I have been recently looking into guice and have the need to have some field injection in my automation framework. For example I have an EnvironmentSetter class which I want to inject as a singleton to various other classes.
1) I do not have a standard main method, so I am struggling with how to bootstrap guice correctly. I am using testNG so I am attempting to bootstrap using a static block like so:
public class TestExecutionListener implements IExecutionListener {
private static final Logger LOG = LogManager.getLogger(TestExecutionListener.class);
static {
Bootstrapper.BootStrapGuiceDI();
}
#Inject
EnvironmentSetter env;
#Override
public void onExecutionStart() {
LOG.debug("Starting test run!");
env.generateEnvironmentProperties();
}
#Override
public void onExecutionFinish() {
LOG.debug("Finished test run!");
}
}
I have also created the following:
public class EnvironmentSetterModule extends AbstractModule {
#Override
protected void configure() {
bind(EnvironmentSetter.class);
}
}
and this is what I am calling from the static block:
public static void BootStrapGuiceDI() {
LOG.debug("Bootstrapping");
Injector injector = Guice.createInjector(new Module());
EnvironmentSetter env = injector.getInstance(EnvironmentSetter.class);
}
In this scenario, my injected EnvironmentSetter env is still null, what do I need in order to use this effectively?
EnvironmentSetter class:
public class EnvironmentSetter implements IEnvironmentPopulator {
private static final Logger LOG = LogManager.getLogger(EnvironmentSetter.class);
PropertyProvider properties = PropertyProvider.INSTANCE;
public EnvironmentSetter() {
}
public void generateEnvironmentProperties() {
Properties props = new Properties();
properties.getAllProperties().forEach((k,v) -> props.setProperty(k,v));
try {
File f = new File("target\\allure-results\\environment.properties");
f.getParentFile().mkdirs();
f.createNewFile();
props.store(new FileOutputStream(f), "Allure Environment Properties");
} catch(IOException ioe) {
LOG.fatal(ioe);
}
}
}
You should be adding the modules you create in the createInejector method not a new Module();.
public static void BootStrapGuiceDI() {
LOG.debug("Bootstrapping");
// Injector injector = Guice.createInjector(new Module()); // use your module (EnvironmentSetterModule )
// Now, guice will be able to "see" your class
Injector injector = Guice.createInjector(new EnvironmentSetterModule());
EnvironmentSetter env = injector.getInstance(EnvironmentSetter.class);
}
Also, only bootstraping it won't make it automatically inject all fields everywhere in test classes, to inject on test you could use your new Injector and inject the members of your test class injectMembers(this), where this would refer to your test instance, so must be executed on some setup block.
Checkout the documentation on guice about how to properly boostrap it on Test => Guice BoundFields
If you are using TestNG, there is a much more simple way to do this using the annotation guiceModule. Basically, TestNG does the bootstrapping for you and all you need to do is mention the Guice module name in the annotation. Example:
#Test(guiceModule = GuiceExampleModule.class)
public class GuiceTest {
#Inject
ExternalDependency dependency;
#Test
public void singletonShouldWork() {
Assert.assertTrue(true, dependency.shouldExecute());
}
}
Read more about this in Cedric's blogpost: TestNG and Guice: a match made in heaven
I have never used guice before, and I wanted to try it out on an example project with jersey based JAX-RS API backed by a service-bean. I followed this guide: http://randomizedsort.blogspot.de/2011/05/using-guice-ified-jersey-in-embedded.html and was able to bring it to work. My setup is very simple, a JAX-RS resource is invoked via Guice and has a field that is annotated #Inject and injected by Guice:
#Path("configuration")
#Produces(MediaType.APPLICATION_JSON)
#Singleton
public class ConfigurationResource {
#Inject
private ConfigurationService configurationService;
So far so good, everything works like it should, besides following: I am using GuiceServletContextListener for setting things up and have to name each component explicitly:
#WebListener
public class GuiceInitializer extends GuiceServletContextListener{
#Override
protected Injector getInjector() {
return Guice.createInjector(new JerseyServletModule() {
#Override
protected void configureServlets() {
//resources
bind(ConfigurationResource.class);
//services
bind(ConfigurationService.class).to(ConfigurationServiceImpl.class);
// Route all requests through GuiceContainer
serve("/management/*").with(GuiceContainer.class);
}
});
}
}
I find it pretty inconvenient to explicitly name all dependencies. I have worked with standalone jersey before and it's perfectly capable of auto-scanning for resources in defined packages. Also Spring and CDI are capable of mapping implementation to interfaces without need to explicitly name them.
Now the question part:
is there any autoscan extension/setting for guice? I found some on the internet, but it's hard to tell which of them are still useable and uptodate.
is there any other possibility to make configuration of implementations and resources more convenient?
thanks in advance.
Leon
I do not think Guice has built in support for someting like the component-scan of Spring framework. However, it is not difficult to simulate this feature in Guice.
You simply need to write a helper module like the following
import com.google.inject.AbstractModule;
import org.reflections.Reflections;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* To use this helper module, call install(new ComponentScanModule("com.foo", Named.class); in the configure method of
* another module class.
*/
public final class ComponentScanModule extends AbstractModule {
private final String packageName;
private final Set<Class<? extends Annotation>> bindingAnnotations;
#SafeVarargs
public ComponentScanModule(String packageName, final Class<? extends Annotation>... bindingAnnotations) {
this.packageName = packageName;
this.bindingAnnotations = new HashSet<>(Arrays.asList(bindingAnnotations));
}
#Override
public void configure() {
Reflections packageReflections = new Reflections(packageName);
bindingAnnotations.stream()
.map(packageReflections::getTypesAnnotatedWith)
.flatMap(Set::stream)
.forEach(this::bind);
}
}
To component scan a package like com.foo and sub packages for classes carrying #Singleton, use it in this way:
public class AppModule extends AbstractModule {
public void configure() {
install(new ComponentScanModule("com.foo", Singleton.class));
}
}
I am trying to learn Guice for dependency Injection using Providers to create multiple instances of an object(Example from getting started guide on Guice website). how should I test this? Please advise.
The following is the module:
package testing;
import com.google.inject.AbstractModule;
public class BillingModule extends AbstractModule {
#Override
protected void configure() {
bind(TransactionLog.class).to(DatabaseTransactionLog.class);
bind(BillingService.class).to(RealBillingService.class);
bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);
}
}
The following is the class under test:
package testing;
import com.google.inject.Inject;
import com.google.inject.Provider;
public class RealBillingService implements BillingService {
private Provider<CreditCardProcessor> processorProvider;
private Provider<TransactionLog> transactionLogProvider;
#Inject
public RealBillingService(Provider<CreditCardProcessor> processorProvider,
Provider<TransactionLog> transactionLogProvider) {
this.processorProvider = processorProvider;
this.transactionLogProvider = transactionLogProvider;
}
public void chargeOrder() {
CreditCardProcessor processor = processorProvider.get();
TransactionLog transactionLog = transactionLogProvider.get();
/* use the processor and transaction log here */
processor.toString();
transactionLog.toString();
}
}
The following is the test class with main():
public class test {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new BillingModule());
BillingService billingService = injector.getInstance(BillingService.class);
billingService.chargeOrder();
}
}
Upon running this, I am expecting the output from the following toString methods to show up but am seeing nothing:
processor.toString();
transactionLog.toString();
What am i missing here?
Please advise,
thanks!
This happens because you just call toString without putting the resulting string anywhere (eg the call to System.out.println)
However providers are not intended to be used like that. You should not call Provider.get yourself: instead require the result of the provider, register your provider and let Guice do its job (you can also annotate methods in your modules with #Provides instead of defining provider classes)
By default providers are called each time a new instance of a certain class is required. Instances are not recycled unless you explicitly request it via using scopes (like the builtin Singleton)
Currently I am using a singleton class to read and load the propertied file. I get an instance of this in any class where I want to use a property value.
Would not it be better to use a static class instead which can be loaded once (when server started or something .. ) instead of using a singleton ? Why and why not ?
Moreover how can we load a static class OnServerStart or when war gets deployed.
PS: Project is web application
Singleton is better for dependency injection and unit testing than statics.
You may inject an instance of singleton class or a Mock of that type to any other class under test.
public class PropertiesHolder {
private static final PropertiesHolder INSTANCE = new PropertiesHolder();
private final Properties props;
private PropertiesHolder() {
props = load();
}
public static PropertiesHolder getInstance() {
return INSTANCE;
}
public String getProperty(String key) {
return props.getProperty(key);
}
private Properties load() {
...
}
}
Then you may mock PropertiesHolder in your test:
#RunWith(MockitoJUnitRunner.class)
public class MyTest {
#Mock private PropertiesHolder holder;
#Test
public void testSomething() {
SomeService service = new SomeService(holder);
when(holder.getProperty("foo")).thenReturn("bar");
String result = service.doSomething();
assertEquals(...)
}
}
For production code you may use:
new SomeService(PropertiesHolder.getInstance());
Or even better, use DI framework, e.g. Spring, for wiring a beans. PropertiesHolder would be a generic bean with factory method getInstance() and the scope 'singleton'.
If you're using Spring in your web application I'd suggest using with it's PropertyPlaceholderConfigurer.
If you don't want to use Spring for that and need to do some actions (e.g. loading property file) on servlet when webapp is started then use ServletContextListener as Bhesh Gurung has suggested.
I understand how to inject a single dependency using Google Guice.
The following snippets are from the Guice site.
To code a configuration the code would be
public class BillingModule extends AbstractModule {
#Override
protected void configure() {
bind(TransactionLog.class).to(DatabaseTransactionLog.class);
bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);
}
}
The component which use the dependencies to be injected shuold looks like the following:
class BillingService {
private final CreditCardProcessor processor;
private final TransactionLog transactionLog;
#Inject
BillingService(CreditCardProcessor processor,
TransactionLog transactionLog) {
this.processor = processor;
this.transactionLog = transactionLog;
}
public Receipt chargeOrder(PizzaOrder order, CreditCard creditCard) {
...
}
}
FInally, the client code would use Guice to inject the dependencies where needed:
public static void main(String[] args) {
Injector injector = Guice.createInjector(new BillingModule());
BillingService billingService = injector.getInstance(BillingService.class);
...
}
My question is:
Is there a built-in feature in Guice to inject not only -for example- BillingService.class but another different implementation in runtime?
I think I can implement the variation of the different classes to be injected thru reflection or some manual mechanism like reading a configuration file to indicate which class to inject but I still want to know if this can be done from Guice itself.
You can make BillingService an interface and bind a different implementation of it decided in runtime in Module's configure method.