guice and app configuration

2,748 views
Skip to first unread message

Stranger in the Q

unread,
Apr 9, 2015, 1:55:37 PM4/9/15
to google...@googlegroups.com

https://bitbucket.org/strangerintheq/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'...

Olivier Grégoire

unread,
Apr 9, 2015, 3:05:55 PM4/9/15
to google...@googlegroups.com
It's not really clear to me.

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/28f51f39-e29a-4f9c-a9ea-c70749adb8f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tim Boudreau

unread,
Apr 10, 2015, 2:59:55 AM4/10/15
to google...@googlegroups.com
FWIW, my Giulius library (heavily inspired by some of Eelco Hillenius' work) - https://github.com/timboudreau/giulius - which is for setting up property injection using @Named, has a little-used feature that sounds like what you're after.

In the general case, you feed it some properties files and default values.  It also has a @Namespace annotation you can use over a class or as a package-annotation, so to specify a specific file-name/collection of settings to use for injection into that class or package.  I added that feature in order to Guicify a legacy codebase that was littered with classes that all contained properties loading code for various file, so that first a Settings (like properties only read only) could be injected, and later the mechanism that located the properties files could be replaced.

So basically you do something like
Settings fooSettings = new SettingsBuilder("foo").add("someProp", "someValue").add(new File("/etc/foo.properties").build();
Settings barSettings = new SettingsBuilder("bar").add("someProp", "otherValue").add(new File("/etc/bar.properties").build();
// You can layer in default values, system properties, env vars, etc.
Injector inj = new DependenciesBuilder().add(fooSettings, "foo").add(barSettings, "bar").build().getInjector();

@Namespace ("foo")
public class A {
   @Inject A(@Named("someProp") String prop) {
      assert "someValue".equals(prop);
   }
}
@Namespace ("bar")
public class B {
   @Inject A(@Named("someProp") String prop) {
      assert "someValue".equals(prop);
   }
}

(you could also annotate A and B with @Defaults and not have to set up the default values when creating the settings)

A unit test may be worth a thousand words here, so:

-Tim

Nate Bauernfeind

unread,
Apr 10, 2015, 11:41:03 AM4/10/15
to google...@googlegroups.com
Especially commented out unit tests! Though a large majority of the thousand words are expletives. ;)

Nate

Cédric Beust ♔

unread,
Apr 10, 2015, 12:53:50 PM4/10/15
to google...@googlegroups.com
I also posted some work I did in this area a while ago:


-- 
Cédric


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.

Jens

unread,
Apr 10, 2015, 2:13:16 PM4/10/15
to google...@googlegroups.com, ced...@beust.com
Whats wrong with Names.bindProperties(binder, myProps) provided by Guice? Isn't that effectively the same?


-- J.

Tim Boudreau

unread,
Apr 10, 2015, 7:40:44 PM4/10/15
to google...@googlegroups.com, ced...@beust.com
On Friday, April 10, 2015 at 2:13:16 PM UTC-4, Jens wrote:
Whats wrong with Names.bindProperties(binder, myProps) provided by Guice? Isn't that effectively the same?

Nothing, but there's a little more to it with Giulius:

 - Layering sets of properties on top of each other (e.g. layer $JAR/META-INF/settings/generated-foo.properties, /etc/foo.properties, ~/foo.properties, ./foo.properties) on top of each other so you can have defaults (annotation generated), system-wide, per-user and per-install properties, and parsing sendopts-style command-line argumenst (--foo bar) overlaid over that

 - Optional periodic refresh of file-based properties

 - Ability to replace properties files with loading config from a centralized config server such as etcd - https://github.com/timboudreau/giulius-settings-etcd - or zookeeper or similar, without major surgery

 - Ability to use @Namespace to choose the source of bound properties (i.e. when injecting @Named, giulius will check what class/package it's injecting into and select the right Settings to pull from) - useful in old codebases that read from heterogenous properties files

 - Having a test harness that lets test methods take injected arguments, which understands this stuff (i.e. you can override properties for a test using annotations or a properties file with the same name as the test) - https://github.com/timboudreau/giulius-tests

-Tim

Reply all
Reply to author
Forward
0 new messages