Monday, October 13, 2014

Software configuration with Spring Boot

In this blog post I would like to show you the configuration possibilities of a Spring bean's name property in combination with Spring Boot. Let's consider the following simple bean.

The @Value("${greeting.name:World}") means that the name can be configured via the greeting.name property and has the default value of "World". You can quickly try it by cloning my example repository which I have created for this blog post and accessing the http://localhost:8080

With the help of Spring Boot Maven Plugin this example is creating a very simple war artifact starting an embedded tomcat instance.
Now let's see what configuration options we have.
We can configure the name property using a command line argument.

We can set it also via a system property.

Or we can use an OS environment variable.

Here you can see that I have used underscore since the OS does not allow me to use period-separated key name. But is not a problem for Spring Boot, it can match it.
The Spring Boot Actuator module's /env endpoint can be very useful in analysing used configuration.

We could also set it via a JNDI attribute. In order to demonstrate it, I will use Wildfly (formerly known as JBoss AS). Just drop the generated war file into your /standalone/deployments and after you have started the server (/bin/standalone.sh) add a JNDI binding via JBoss CLI tool.

And accessing the http://localhost:8080/configuration-with-spring-boot-0.0.1-SNAPSHOT/ you will see the name property was set via JNDI. The interested reader can have a look what modifications I needed to make to able to deploy it to JBoss EAP 6.3.0.

Another option is to set it via an external property file. By default it uses the application.properties, however you can easily override it via spring.config.name as shown below.

You can group configuration in profiles. With Spring profiles you can achieve that you have one deployable artifact across development, staging and live environments, running on your laptop, application server or on a PaaS provider. Achieving this makes testing very easy.

Lastly I would like to show you how Spring Boot can help in setting up logging configuration. Spring Boot already provides a default base configuration for each logging implementation that you can include if you just want to set levels. The base configuration is set up with console output and file output (rotating, 10 Mb file size) which is usually enough.

With the logging.file you can configure the file output location.
What you would however mostly do is to setup an externalised logging configuration. For logging I recommend logback. It can automatically reload logging configuration upon modification. The external logging configuration you can set via the logging.config property.

You should also customise the banner for your app :) I used this tool.



Hope you see the great flexibility regarding configuration when using Spring Boot. There are other goodies like using YAML instead of properties files, which is a very convenient format for specifying hierarchical configuration data.

4 comments:

Amit said...

Nice post. Can you refer me to a introductory article/video to spring boot? I would like to understand what is it and what problems does it solve?

Zoltan Altfatter said...

Here you have it http://projects.spring.io/spring-boot/

Josh Long said...

May I (humbly) recommend https://www.youtube.com/watch?v=eCos5VTtZoI as an introductory tutorial for Spring Boot? It's a 1hr talk. I hope this helps..

Zoltan Altfatter said...

Great! Thanks Josh.