{% extends "../../layout.html" %} {% block sectionClasses %}plugins hasBreadCrumb plugins-spincast-config-props-file{% endblock %} {% block meta_title %}Plugins - Spincast Properties File Config{% endblock %} {% block meta_description %}Allows an application to define its configurations in a .properties file.{% endblock %} {% block scripts %} {% endblock %} {% block body %}

Overview

Allows an application to define its configurations in an external .properties file. You can then easily have different configurations, depending on the environment the application runs on. You can also use this file to override the Spincast configurations without the need to provide a custom SpincastConfig implementation class.

The path to the .properties depends on the configurations in place. By default :

  • The plugin tries to find an "app.properties" file in the directory where the application .jar is running.
  • If no "app.properties" file is found next to the .jar, or if the application is not running from a .jar file (the code may be running inside an IDE, for example), then the default configurations are used.

If you enable the getSpecificPathMainArgsPosition() option using configuration, then Spincast will first look at the application's arguments to get the path to the .properties file. For example :

java -jar mySpincastApp.jar /var/www/mySite/mySite.properties

You can also override the lookForPropsFileSpecificPath() method if you want to use another way of specifying the path (for example by looking at an environment variable).

Installation

If you use the spincast-default artifact and the standard Bootstrapper, register this plugin using :

public static void main(String[] args) {

    Spincast.configure()
            .plugin(new SpincastConfigPropsFilePlugin())
            //...
            .init();
}

If you start from scratch, using the spincast-core artifact, you can use the plugin by :

1. Adding this Maven artifact to your project:

<dependency>
    <groupId>org.spincast</groupId>
    <artifactId>spincast-plugins-config-properties-file</artifactId>
    <version>{{spincast.spincastCurrrentVersion}}</version>
</dependency>

2. Installing the provided SpincastConfigPropsFilePluginModule module to your Guice context.

If you use a custom configuration class (which is recommended), you make this class extend SpincastConfigPropsFileBased :

public class AppConfig extends SpincastConfigPropsFileBased implements AppConfig {

    @Inject
    public AppConfig(SpincastUtils spincastUtils,
                     @MainArgs @Nullable String[] mainArgs,
                     @Nullable SpincastConfigPropsFilePluginConfig pluginConfig) {
        super(spincastUtils, mainArgs);
    }

    @Override
    public String getAppName() {
        return getConfig("app.name");
    }

    @Override
    public int getSomeNumericConfig() {
        return getConfigInteger("app.some.other.config", 12345);
    }
}

Explanation :

  • 1 : Our custom configuration class extends SpincastConfigPropsFileBased but also implements our custom AppConfig interface.
  • 3-8 : Constructor required to satisfy the base class.
  • 10-13 : A first application specific configuration. We use the getConfig(key) method to get the value to use from the .properties file. Since no default value is provided, an exception is throw if the configuration is not found!
  • 15-18 : A second application specific configuration. Here we use getConfigInteger(key, defaultValue) to get an Integer configuration. If the configuration is not found in the .properties file, the specified default value is used.

Plugin class

The class implementing the SpincastPlugin interface is SpincastConfigPropsFilePlugin.

Configuration

You can bind a SpincastConfigPropsFilePluginConfig implementation to tweak the default configurations used by this plugin. By default, the SpincastConfigPropsFilePluginDefault class is used as the implementation.

Using free keys

If you want to be able to access your configurations without having to define a method for all of them, you can use the provided FreeKeyConfig interface:


public static interface AppConfig extends SpincastConfig, FreeKeyConfig {

    public String someTypedConfig1();
    public Boolean someTypedConfig2();
    
    //...
}

Now, you have access your .properties based configurations without having to define a method for all of them. You can also use the getConfig(...), getConfigInteger(...) or getConfigBoolean(...) methods :

AppConfig configs = getAppConfig();

String config = configs.getConfig("some.config.as.string");
config = configs.getConfig("some.non.existing.config", "default value");

Integer configInt = configs.getConfigInteger("some.config.as.int");
configInt = configs.getConfigInteger("some.non.existing.config", 42);

Boolean configBool = configs.getConfigBoolean("some.config.as.bool");
configBool = configs.getConfigBoolean("some.non.existing.config", true);

// Typed configs still work too, of course!
config = configs.someTypedConfig1();
configBool = configs.someTypedConfig2();

Spincast configuration keys

In your .properties file, you can override some default Spincast configuration by using those keys :

  • spincast.environment.name
    The name of the environment.
  • spincast.environment.isDebug
    Should debug mode be enabled? ("true" or "false")
  • spincast.server.host
    The host/IP the server should bind to.
  • spincast.httpServer.port
    The port of the HTTP server. If not specify or is <= 0 no HTTP server will be started. (Integer value)
  • spincast.httpsServer.port
    The port of the HTTPS (secure) server. If not specify or is <= 0 no HTTPS server will be started. (Integer value)
  • spincast.httpsServer.keystore.path
    The path to the KeyStore, for SSL. Can be a classpath path or and absolute path.
  • spincast.httpsServer.keystore.type
    The type of the KeyStore, for SSL. For example: "JKS".
  • spincast.httpsServer.keystore.storepass
    The "storepass" of the KeyStore, for SSL.
  • spincast.httpsServer.keystore.keypass
    The "keypass" of the KeyStore, for SSL.

Example .properties file

# Override some default Spincast configurations
spincast.environment.name=prod
spincast.environment.isDebug=false

# App specific configurations
app.name=My supercalifragilisticexpialidocious app!
app.some.other.config=42

Javadoc

{% endblock %}