{% 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 %}
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 :
"app.properties" file in the directory where the application .jar is running.
"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).
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 :
SpincastConfigPropsFileBased but also implements
our custom AppConfig interface.
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!
getConfigInteger(key, defaultValue)
to get an Integer configuration. If the configuration is not found in the
.properties file, the specified default value is used.
The class implementing the SpincastPlugin interface is SpincastConfigPropsFilePlugin.
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.
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();
In your .properties file, you can override some default Spincast
configuration by using those keys :
spincast.environment.name
spincast.environment.isDebug
true" or "false")
spincast.server.host
spincast.httpServer.port
<= 0
no HTTP server will be started. (Integer value)
spincast.httpsServer.port
<= 0
no HTTPS server will be started. (Integer value)
spincast.httpsServer.keystore.path
KeyStore, for SSL. Can be a
classpath path or and absolute path.
spincast.httpsServer.keystore.type
KeyStore, for SSL. For example: "JKS".
spincast.httpsServer.keystore.storepass
KeyStore, for SSL.
spincast.httpsServer.keystore.keypass
KeyStore, for SSL.
.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