1

I need to read config.properties file from location that i set with variable -Dapp.conf=/path/to/config.properties and set it to a Datasource when i launch my application. file should be at any location within filesystem. how to do this?

Igor
  • 478
  • 9
  • 22

1 Answers1

2

You can load your properties file as next:

Properties p = new Properties();
try (Reader reader = new FileReader(System.getProperty("app.conf"))) {
    p.load(reader);
}

Once loaded you can use your properties instance to set your datasource configuration.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • how to set this variable when i launch my application? I use maven – Igor Apr 21 '16 at 21:53
  • you mean the system property? how to you launch your application with maven ? – Nicolas Filotto Apr 21 '16 at 21:55
  • If so check this http://stackoverflow.com/questions/3708846/how-to-pass-systemproperties-when-invoking-execjava-plugin-in-maven – Nicolas Filotto Apr 21 '16 at 21:56
  • i launch my app with Jetty from Idea plugin, in the `runner` maven properties, in `Environment variables` i set `-Dapp.conf=/path/to/config.properties`. – Igor Apr 21 '16 at 22:03
  • Nicolas, i did as you said and this was exactly what i needed! Thank you! – Igor Apr 21 '16 at 22:09
  • How can i insert properties from my `config.properties` into a DataSource defined in `appContext.xml` ? i'm trying `` but it doesnt work i'm trying to load config with this` ` – Igor Apr 22 '16 at 00:59
  • check this https://sethuramanmurali.wordpress.com/2013/05/04/load-properties-file-using-spring-context/ – Nicolas Filotto Apr 22 '16 at 06:29