Documentation / Setup / ServerConfig
Overview
Configuration of EbeanServer instances is defined by the ServerConfig
bean. It has a series of
getters and setters for all the configuration options available.
In code terms a new ServerConfig instance is created and configured by either caller setter methods or
loading configuration from ebean.properties or external properties. Finally EbeanServerFactory
is used to take the ServerConfig and create an EbeanServer
instance.
ebean.properties
The ServerConfig.loadFromProperties()
loads configuration from ebean.properties
file.
This is can be used where some properties are set via code (calling setter methods on ServerConfig)
and some from ebean.properties.
ServerConfig config = new ServerConfig();
config.setName("pg");
...
// load configuration from ebean.properties
// using "pg" as the server name
config.loadFromProperties();
...
EbeanServer server = EbeanServerFactory.create(config);
External properties
You can have an external mechanism for loading Properties
and supply this to ServerConfig via:
ServerConfig.loadFromProperties(Properties)
. This is can be used where some configuration is set
via the externally loaded properties and some is set via code (calling setter methods on ServerConfig).
// load properties externally
Properties externalProps = ...;
ServerConfig config = new ServerConfig();
config.setName("pg");
...
// load configuration from external properties
// using "pg" as the server name
config.loadFromProperties(externalProps);
...
EbeanServer server = EbeanServerFactory.create(config);
Register
By default ServerConfig has register=true and this means that the EbeanServer instance is registered with
the Ebean singleton and can be later accessed via Ebean.getServer(serverName)
You need to explicitly ServerConfig.setRegister(false) if you don't this feature.
ServerConfig config = new ServerConfig();
config.setName("pg");
...
EbeanServer server = EbeanServerFactory.create(config);
...
// Later on we can get the server instance
// using it's name via Ebean
EbeanServer server = Ebean.getServer("pg");
Default server
You can set ServerConfig.setDefaultServer(true) to indicate that the EbeanServer instance created should
be the 'default' EbeanServer and can be later accessed via Ebean.getDefaultServer()
ServerConfig config = new ServerConfig();
config.setName("pg");
config.setDefaultServer(true);
...
EbeanServer server = EbeanServerFactory.create(config);
...
// Later on we can get the server instance ...
EbeanServer server = Ebean.getDefaultServer();
// Note: serverName of null is the same as getting the default server
EbeanServer server = Ebean.getServer(null);
Dependency injection
For dependency injection with Guice, Spring and similar DI frameworks typically a "provider" or "factory"
is required that creates a ServerConfig, configures it as required perhaps using ebean.properties or
external properties and then uses EbeanServerFactory
to create the EbeanServer instance.
Inject and ActiveRecord
It is common and expected that you want to be able to both @Inject
EbeanServer
instances in the traditional DI coding style and also use the active record style. This can be done
when the EbeanServer instances are created with register
as true and one instance marked
as the default server
.