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.


Angus said...
This comment has been removed by the author.
Angus said...

Nice post. Thank you for taking the time to write this up. And thank you for changing the layout. The other version of the bookshelf kept pulling my focus making your blog to read.

enriquezreneblog said...
This comment has been removed by the author.
enriquezreneblog said...

Hi, I have deployed on wildly however when I'm trying to call the rest services I have the 404 error

Whats is a mapping, I'm trying


hari andro said...

I am regular visitor of this blog .I am working as blog reviewer in a private press and I saw many useful posts here. Sure, I will give best ratings for this blog .Keep posting best posts like this to get top reviews and ratings from blog reviewers and people .And I am thankful for this valuable post.
Nautical Science Colleges in Chennai | Mechanical Colleges in Chennai | ECE Colleges in Chennai

Unknown said...

Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much

Personal Installment Loans
Payday Cash Advance loan
Title Car loan
Cash Advance Loan

Unknown said...

It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
Android Training in Chennai
Ios Training in Chennai

Pawel Co Faddey said...

Great Article
Projects for ECE Final Year Students Final Year Project Centers in Chennai

Shanthi Cabs said...

Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. Such a nice blog you are providing ! Kindly Visit Us @ Best Travels in Madurai | Tours and Travels in Madurai | Madurai Travels

Tony Chew said...

Tetapi ada sejarahwan lainnya ada yang yakini kalau orang pertama yang membuat permainan itu bernama Keung Tai Kung di era 12 SM.
bandar ceme terbaik
paito warna
bocoran sgp
data hk

Paul Delphine said...

Are you looking for a business loan, personal loans, mortgage loans, car loans, student loans, unsecured consolidation loans,project funding etc ... Or simply refuse loan from a bank or financial institution for one or more reasons? We are the right solutions for credit! We offer loans to businesses and individuals with low and affordable interest rate of 2%. So if you are Interested in an urgent and secured loan. For more information kindly email us today Via:

PoL said...

Do you want a fresh way of publishing your articles on the internet? If so, you should consider using our news website templates since are was designed and developed specifically to meet that need.

Unknown said...

paito warna china
paito warna singapore
paito warna sydney
syair sydney
paito warna terlengkap
live hk 6d
live draw hk

Jackie Co Kad said...

The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Project Centers in Chennai for CSE.

Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai