java - How can I inject properties from a properties file into a method using Guice? -


basically want load properties file singleton , use guice inject properties on fly class or method requires them.

my module looks this:

public class configurationmodule extends abstractmodule {   @override   protected void configure() {     try{       classloader classloader = thread.currentthread().getcontextclassloader();       properties properties = new properties();        properties.load(classloader.getresourceasstream("filename.properties"));       names.bindproperties(binder(), properties);      } catch(exception e){       e.printstacktrace();     }   } } 

then in program declare methods like:

public class testclass {   @inject   public void testmethod(@fooserveraddress string fooserveraddress){     //do things   } } 

where fooserveraddress = "blah" in filename.properties.

now know can instantiating testclass guice this:

injector injector = guice.createinjector(new configurationmodule()); testclass testclass = injector.getinstance(testclass.class); 

but doesn't work me because want use singleton. doing way means have use guice injector whenever instantiate class can't since of methods called outside scope.

so there way can use guice automatically load properties property file method?


Comments