Saturday, October 25, 2014

Datasource configuration with Spring Boot

For configuring a datasource using Spring Boot we have couple of options. With the help of a simple example project, which is available on my github profile, I will walk you through these options. The example contains two simple entities Portal and Page with a 1:N bidirectional relationship.

The example with the help of Spring Data JPA and REST modules, creates and exposes CRUD operations for these entities using plain HTTP REST semantics.

In development mode it is very convenient to use an in-memory database. By simply including H2 as a maven dependency, Spring Boot uses it as an in-memory database, no connection URL needs to be provided. This example uses the Spring Data JPA interface to populate the database with some example data.

Using the @Profile("default") this bean will be just initialised in the default Spring profile (when no other profile is specified). We could have created an explicit profile for development mode also, but this example is just using the default for this.
Next we just need to start the application. With the following glue code we can start it as a standalone application (java -jar). It will also work if we drop the produced war file in a Servlet 3.0 servlet container.

Setting to debug level the org.hibernate.SQL logger, we can see the that database schema was created and populated during application startup.

In a production environment however is unlikely that we need to create the database schema and populate the database. We just need to tell to Spring Boot where and how to connect to the database. Since we would like to keep the ability to work with the app in development mode, the solution is to use another Spring profile for the production setup. Creating the configuration with the below properties (using mysql in this example) we can instruct Spring Boot to switch to production mode by setting the to production

As you can see the jdbc driver class name is not needed, Spring Boot can infer it from the database URL. Have a look at the DriverClassNameProvider for the details.
With the next release of Spring Boot (in 1.2.0-SNAPSHOT is already available) it will be possible to reference the datasource via JNDI, which is very common in traditional deployment setups where the datasources are configured inside the application server. Of course we can do this already with Spring using JndiDataSourceLookup but it got easier using Spring Boot, without using infrastructure beans. In order to demonstrate this, I have deployed the application into JBoss Wildfly, and created another Spring profile called jboss with the following configuration

Using the default standalone.xml as the initial configuration the datasource connecting to MySQL can be added like in the example below.

We also need to create a module.xml in the JBOSS_WILDFLY_HOME/modules/com/mysql/main folder with the following configuration. The mysql jdbc driver needs to be put into this folder also.

Next, by dropping the created war file into JBOSS_WILDFLY_HOME/standalone/deployments folder and executing the following command we can startup this simple application in JBoss.

Accessing the http://localhost:8080/datasource-configuration-1.0-SNAPSHOT/ you will see that the data is loaded from MySQL end exposed with Spring Data REST. If you would like to try it out this example, have a look at my github profile.

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("${}") means that the name can be configured via the 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/ 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, however you can easily override it via 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.