XML Free Spring Hibernate Configuration


After a lot of digging on the web I couldn’t find any complete tutorial or example for an XML free Spring Hibernate Configuration tutorial to integrate Spring 3.X with Hibernate 4.X. So here I present a fully working simple tutorial for those who want to get rid of the XML configurations for Hibernate and Spring integration.

Here is a link to my latest post, which develops a spring application from scratch, using annotation based spring, and hibernate.

Purpose of the Article

This article is to explain in the easiest possible manner the ways to integrate Hibernate 4.x with Spring 3.x without XML configurations. I will write another article for the XML lovers later.


I always emphasize on using the compatible jars for all the possible technologies and frameworks. So I would recommend you to download hibernate distribution from here and download spring distribution from here

Now after including both the distributions you might need some more jars listed below:

  • mysql-connector-java-5.1.6.jar – for database driver of MySQL
  • commons-dbcp.jar – database connectivity pool
  • commons-pool.jar
  • commons-logging-1.1.1.jar
  • aopalliance-1.0.jar

The Domain Model

FIRST_NAME varchar(45) NOT NULL,
LAST_NAME varchar(45) NOT NULL,
AGE int(11) NOT NULL,

If possible populate it with some values.

Model class corresponding to Domain Model

The Annotated Application Configuration

This will be the equavalent to the traditional spring-context.xml files in the older generation of Spring. Below is the code for the same and we will try and revisit the meaning of each line here:

@Configuration – is equivalent to and it tells that this class should be considered as one of the configuration classes.
@EnableTransactionManagement – this is the alternative to in spring xml.
@PropertySource(“classpath:app.properties”) – means load a properties file app.properties which is present in the classpath.
@ComponentScan(basePackages = “com.test”, excludeFilters = { @ComponentScan.Filter(Configuration.class) }) – is equivalent to which means it will scan all the packages whose base package is com.test

the Environment class serves as the property holder and stores all teh properties loaded by the @PropertySource

this configures the dataSource for the SessionFactory.

This configures the session factory and also serves as the alternative to hibernate’s hibernat.cfg.xml file. The line factory.setPackagesToScan(new String[] { “com.test.model” }); serves as an alternative to the hbm mapping file.

This creates a transactionManager bean of type HibernateTransactionManager and sets the sessionFactory to it.

Creates a bean for the service layer, this can be later fetched in any class to subsequently invoke the DAO layer methods.

Service Layer

As this is not a complete application and the motive is to enable the reader to udnerstand and bring up a simple application running, we create a service layer interface with just one method to fetch the records.

The implementation for the above interface follows:

Please note the @Transactional annotation, nothing specific about it, if you do not put this annotation you might face some errors. ONe of them is below:

The DAO layer

The DAO layer just fetches the list of persons and returns as it is to the service layer. THe interface for the DAO layer is defined below:

The implementation of the above interface is provided below:

Bootstrapping the application

Lets write a class with a main method which will initiate the whole process

Get the instance of AnnotationConfigApplicationContext created by the AppConfig configuration class, then fetch the service bean from the context and invoke the getPersons() method to fetch the list of persons.

Application properties file

Below is the app.properties file. You can place it in the src folder of your project, it will automatically get into the classpath. You can place it whereever you want, please make sure it is in the classpath.

Source Code

Please find the source code attached here. This doesn’t include any jar file.
Source Code


You can actually use this whole setup in a REST web-service , a web applcaition or any other setup. THe purpose is to show the basic setup and if you run the Bootstrap class you will see the list of persons displayed on the console.

Thank to the spring blog here which helped me complete this article.

Stay connected and stay Subscribed

  • Jay

    Thanks for the article.

    Annotation based config is _so_ much cleaner than half a dozen unreadable XML files scattered all over the place.