Sunday, December 2, 2012

Spring and Thymeleaf with JavaConfig (Part 1)

In this tutorial, we will create a CRUD application based on Spring MVC 3.x and Spring Data JPA. We will utilize JavaConfig instead of XML to configure our application. For the view layer, we will use Thymeleaf as our template engine instead of JSP to process our html pages.

Table of Contents

Click on a link to jump to that section:
  1. Functional Specs
  2. Creating the View
    • HTML Mockup
    • Thymeleaf Integration
  3. JavaConfig
  4. Layers
    • Domain
    • Service
    • Controller
  5. Running the application
    • Clone from GitHub
    • Create the Database
    • Run with Maven and Tomcat 7
    • Run with Maven and Jetty 8
    • Import to Eclipse
    • Validate with W3C


These are the main Maven dependencies:
  • Spring 3.2.0.RC1
  • Spring Data JPA 1.2.0.RELEASE
  • Thymeleaf 2.0.14
  • Hibernate 3.6.3.Final
  • See pom.xml for full details

Required Tools

These are the minimum required tools:
  • Git
  • Maven 3.0.4
  • MySQL
  • Eclipse IDE or SpringSource Tool Suite (STS)

GitHub Repository

There are two versions of the application: a JavaConfig-based and an XML config-based app. Both versions are identical in their feature set.

Functional Specs

Our application's requirements are quite straightforward:
  • Create a simple form to manage user information
  • Provide the following fields: first name, last name, username, role
  • Username must be unique
  • Provide CRUD operations
  • Provide table to view all users

Here's our Use Case diagram:



Here's a screenshot of our working application:


In the next section, we will focus on the view layer. We'll start writing the HTML mockup template; then we'll integrate it with Thymeleaf. Click here to proceed.
StumpleUpon DiggIt! Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring and Thymeleaf with JavaConfig (Part 1) ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share


  1. Thanks for the nice and comprehensive example.
    If possible, please give a sample for Java Config based Spring Security 3.2 or let me know, how to configure Spring Security 3 using Java annotation.

    Thanks in advanced....

    1. Rahi, you're welcome. I'm actually on the process of writing a guide that covers the latest Spring Security in a Spring Social app. It uses JavaConfig and no web.xml. Unfortunately, the declaration of the rules (i.e spring-security.xml) will remain in XML because it's easier and less verbose than with JavaConfig.

    2. Yeah...Thanks a lot Mark..Your Spring Social example is very comprehensive and helpful.

  2. Nice example. While playing with it, and incorporating this pagination example

    I noticed that the users.html template would not update after saving changes when running in STS tcServer (integrated into STS).

    For development purposes, you might consider adding "resolver.setCacheable(false);" to the ServletContextTemplateResolver bean definition in your ThymeleafConfig class. The value (true or false) could be externalized to a properties file, so that for dev it would be false; for production, set the property to true.

    If you are developing and running in STS and you make changes to the template, you will see the update almost immediately -- much better than restarting the server.

  3. Thanks for sharing fabulous information. It' s my pleasure to read it.I have also bookmarked you for checking out new posts. by HRM 531 Week 1

  4. how to display the image using this javaconfig

  5. thank u for giving this best information..we are offering the best spring online training

  6. generate OAuth keys for Facebook and Twitter (Online Java Training) Generate OAuth keys Java Training in Chennai Consumer key and Consumer secret values: J2EE Training in Chennai

  7. Hey amazing tutorial!
    However I would strongly suggest to update it to Spring 4 as Spring 3 is obsolete now.
    David Mayer @

  8. I have read your blog its very attractive and impressive. I like it your blog.

    Spring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training

    spring training in chennai spring hibernate training in chennai

  9. nice blog. thanks for sharing valuable information. It’s really good.Any one looking software courses nareshit is the best offline and online software training institute in Hyderabad and Chennai.

  10. Thanks for your article .its very useful for everyone.Spring and java config was really useful. best aws training in chennai | aws course fees details

  11. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    core java training in Electronic City

    Hibernate Training in electronic city

    spring training in electronic city

    java j2ee training in electronic city

  12. Nice blog. thanks for sharing valuable information. It’s really good.Any one looking software courses. hit the below links