Load config from filesystem

Load configuration from somewhere on the filesystem

Loading from the filesystem is slightly trickier than loading from the classpath because the filesystem varies between environments.

Full source and tests for this example can be found here.

package org.simple;

import org.yarnandtail.andhow.*;
import org.yarnandtail.andhow.property.StrProp;

/** This class will be found at runtime and used to initiate AndHow */
public class CustomInit implements AndHowInit {
  public static final StrProp PROP_FILE_ON_FILESYSTEM = StrProp.builder()
   .desc("Path to a properties file on the filesystem, "
    + "in form appropriate to the local system. "
    + "If specified, it must exist.").build();

 @Override
 public AndHowConfiguration getConfiguration() {
  return  StdConfig.instance()
    .setFilesystemPropFilePath(PROP_FILE_ON_FILESYSTEM);
 }
}

The path to a property file on the filesystem will vary from machine to machine, tier to tier, so it cannot be hardcoded - it must be a Property that can be configured.

To tell AndHow about that property, you must implement the AndHowInit interface. AndHow will find that implementation at runtime and call getConfiguration() on it to configure itself. You can build that configuration to have a property pointing to a file path - Just make sure the value for that property is loaded prior to the StdPropFileOnFilesystemLoader. The load order is listed on the home page.