{#========================================== Spincast Properties File Config ==========================================#} {% extends "../../layout.html" %} {% block sectionClasses %}plugins 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 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/mySiteApp.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 explict path is provided, the plugin tries to find an app.properties
file in the same directory where the application's .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 is running inside an IDE, for example), then the
default configurations are used.
You can add your application specific configurations in this .properties file, but
by using the default keys,
you can also override Spincast default configurations.
Note that you don't have to override all default configurations. If
a particular Spincast configuration is not there, its default value will be used.
Add this artifact to your project:
<dependency>
<groupId>org.spincast</groupId>
<artifactId>spincast-plugins-config-properties-file</artifactId>
<version>{{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 IAppConfig {
@Inject
public AppConfig(ISpincastUtils 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 :
SpincastConfigPropsFileBased but also implements
our custom IAppConfig 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.
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(IDefaultRequestContext.class)));
Or, if you use a custom Guice module and request context type:
Injector guice = Guice.createInjector(Modules.override(new AppModule(args))
.with(new SpincastConfigPropsFilePluginGuiceModule(IAppRequestContext.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()));
}
//...
}
String getConfig(String key)
String getConfig(String key, String defaultValue)
Boolean getConfigBoolean(String key)
Boolean getConfigBoolean(String key, Boolean defaultValue)
Integer getConfigInteger(String key)
Integer getConfigInteger(String key, Integer defaultValue)
In your .properties file, you would add any application specific configuration,
but you can also override default Spincast configurations. Here are the keys to use to
override those.
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
# Default Spincast configurations spincast.environment.name=prod spincast.environment.isDebug=false spincast.httpServer.port=-1 spincast.httpsServer.port=443 spincast.httpsServer.keystore.path=/var/www/mySite/certificates/myKeyStore.jks spincast.httpsServer.keystore.type=JKS spincast.httpsServer.keystore.storepass=myStorePass spincast.httpsServer.keystore.keypass=myKeyPass # App specific configurations app.name=My supercalifragilisticexpialidocious app! app.some.other.config=42