Stack Exchange
Using Google Guice to inject java properties
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? 


is there a reason you're new'ing them manually as opposed to using guice to inject them into your test class (which would be the normal way)?

You mean why "TestConfiguration config = new TestConfiguration();"? Can you give an example how to do it in another way?

@markus: No, not TestConfiguration... it's normal to new modules. The question is about TestImpl creating other classes where you also need to inject properties. Normally, you would declare those other classes (or Providers of them) as dependencies of TestImpl so Guice can create them rather than you creating them with new in TestImpl.

It's not clear to me how to do this. Assume I want to create the        class public class TestExtension {
@Inject
public TestExtension(@Named("Property1") String property1, @Named("Property2") Integer property2) {
System.out.println(property1);
System.out.println(property2);
}
}
How can I tell guice to create it?

have you read the Getting Started guide? This tells you how to get going with guice and how to setup a simple binding.


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.
