Friday, December 20, 2013

Spring MVC 3 + Spring Security + Hibernate 4 tutorial

This tutorial helps you to create a spring mvc application integrated with hibernate 4 and Spring security. Before continuing read through my previous article on Spring MVC 3 - Hello world example

The tutorial uses MySQL as database and source code enhanced from the previous Hello world example.

Changes to your pom
Add/modify the following to the properties section of your pom.xml. The spring version is increased to 3.2.3 to support transactions.
Add the following to the dependencies section

Now we are set to use hibernate and spring security in your application.

Create file application-security.xml inside your webapp >> WEB-INF >> spring folder and paste this content.
Replace the following to your servlet-context.xml. The changes include adding necessary namespaces and made beans as default namespace for convenience.
Create a new file controllers.xml Now all the configurations are done.  Create your controllers and views, I have not explained the views, web.xml for simplicity. The complete sample can be found here in github
Create User.java with the following content, make sure your test database has an user table with fields that matches the properties in the below
Create UserController.java with the following content
Create UserDAO.java with the following content
Download the complete code in github