Search code examples
javaguicegovernator

Guice load properties from System Properties


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


Solution

  • 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").