{#========================================== Spincast Properties File Config ==========================================#} {% 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 have different configurations, depending on the environment the application runs on.

The path to this file can be explicitly defined: by default, the first argument of the main(...) method, if present, is considered as the path to this configuration file. For example:

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

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

If no explicit path is provided when starting the application, the plugin tries to find an "app.properties" file in the directory where the application .jar is running.

If no explicit path is provided when starting the application and 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.

Note that by using the Spincast configuration keys, you can also override some Spincast default configurations in that file.

Installation

Add this artifact to your project:

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

If you use a custom configuration class (which is probably the case), you don't have to install any Guice module for this plugin, and you simply make your configuration class extend SpincastConfigPropsFileBased:

public class AppConfig extends SpincastConfigPropsFileBased implements AppConfig {

    @Inject
    public AppConfig(SpincastUtils spincastUtils,
                     @MainArgs @Nullable String[] mainArgs) {
        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-7 : Constructor required to satisfy the base class.
  • 9-12 : 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!
  • 14-17 : 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.

If your application doesn't have a custom configuration class and you simply want to be able to externally override some of Spincast default configurations (for example the server port), then bind the provided SpincastConfigPropsFilePluginGuiceModule Guice module:

Injector guice = Guice.createInjector(Modules.override(new SpincastDefaultGuiceModule(args))
        .with(new SpincastConfigPropsFilePluginGuiceModule(DefaultRequestContext.class, 
                                                           DefaultWebsocketContext.class)));

Or, if you use a custom Guice module, request context type and WebSocket context type:

Injector guice = Guice.createInjector(Modules.override(new AppModule(args))
        .with(new SpincastConfigPropsFilePluginGuiceModule(AppRequestContext.class, 
                                                           AppWebsocketContext.class)));

You can also override the default configuration plugin installation, from your custom Guice module:

public class AppModule extends SpincastDefaultGuiceModule {

    public AppModule(String[] mainArgs) {
        super(mainArgs);
    }

    @Override
    protected void configure() {
        super.configure();
        //...
    }

    @Override
    protected void bindConfigPlugin() {
        install(new SpincastConfigPluginGuiceModule(getRequestContextType(),
                                                    getWebsocketContextType()));
    }
    //...
}

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 %}