Guice load properties from System Properties

Viewed 69

I am using Guice without any spring dependencies.

Is there way to get System properties passed to java (java -Dfoo="asda") in Module. The goal is to load properties from outside of jar file.

Most examples so far use preexisting properties file

Similar to this question: Guice properties injection

2 Answers

Use Names.bindProperties()

Usage is very easy:

@Override protected void configure() {
  Names.bindProperties(binder(), System.getProperties());
}

Then each time you inject @Named("java.user.home") String you'll get the value of System.getProperty("java.user.home").

used System.getProperty() as temprary solution. But hope there is more elegant way

Related