-
Notifications
You must be signed in to change notification settings - Fork 211
Defining Properties
Microserver allows properties to be defined either via a properties files or annotation
Microserver will try to find three different property files on startup. They are :-
- application.properties (name configurable via Microserver annotation)
- service-type.properties (name configurable via Microserver annotation)
- instance.properties (name configurable via Microserver annotation)
instance.properties overrides servicer-type.properties which overrides application.properties.
Conceptually the difference is to allow a one property file to act as a shared properties file, while leaving another to be used on a per instance basis. If you do not intend to share properties files among services (best practice for Production systems), then you can choose and use only one.
Annotation based property configuration is primarily to provide a convenience during early development.
E.g. to set the hibernate properties for a class
@Microserver( entityScan = "app.jdbc.roma.spring.data.com.aol.micro.server", properties = {
"db.connection.driver", "org.hsqldb.jdbcDriver", "db.connection.url",
"jdbc:hsqldb:mem:aname", "db.connection.username", "sa",
"db.connection.dialect", "org.hibernate.dialect.HSQLDialect",
"db.connection.ddl.auto", "create-drop" })
The properties field on the Microserver and Microboot annotations allows properties to be explicitly defined.
The default property name is also configurable via the Microserver and Microboot annotations
@Microserver(propertiesName="coolNewApp.properties")
Microserver uses Spring to handle Properties and thus properties are injectable into all Spring beans via the
@Value
annotation. Defaults can be specified using a colon
e.g. @Autowired public Constructor(@Value("my.property.name:defaultValue") String myProperty)
myProperty will be injected with a default value of 'defaultValue'.
Microserver supports auto-discovery of application.properties. Microserver will assume a default file name of 'application.properties'. Microserver will check for a properties in the following order
-
System property 'application.property.file' and if present will load the property file from disk using that.
-
Otherwise Microserver will look for a System Property 'application.env' and will load the application property file from the classpath using the resource name 'application-${application.env}.properties.
-
Alternatively Microserver will load application.properties directly from the classpath.
-
If still not found Microserver will load application.properties from disk in the current directory
The default file name application.properties can be configured by exception (use PropertyFileConfig.setApplicationPropertyFileName(String filename).
Microserver application properties loading is configured by the class PropertyFileConfig. You can replace this with your own Spring configuration file to load property files by a different set of rules (by passing in your class to the constructor of Microserver).