Skip to content

Defining Properties

johnmcclean-aol edited this page Nov 12, 2015 · 3 revisions

By file & annotation

Microserver allows properties to be defined either via a properties files or annotation

Properties file configuration

Microserver will try to find three different property files on startup. They are :-

  1. application.properties (name configurable via Microserver annotation)
  2. service-type.properties (name configurable via Microserver annotation)
  3. 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 configuration

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'.

Property file discovery

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

  1. System property 'application.property.file' and if present will load the property file from disk using that.

  2. 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.

  3. Alternatively Microserver will load application.properties directly from the classpath.

  4. 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).

Clone this wiki locally