Auditing Entities in Spring Data MongoDB

Spring Data MongoDB 1.2.0 silently introduced new feature: support for basic auditing. Because you will not find too much about it in official reference in this post I will show what benefits does it bring, how to configure Spring for auditing and how to annotate your documents to make them auditable Auditing let you declaratively tell Spring to store:


First of all Maven dependencies to latest Spring Data MongoDB and Spring Data Commons. Additionally in order to use date-related audit annotations we need to add joda-time to classpath.




In order to enable auditing we need to add <mongo:auditing/> to Spring configuration. Currently there is no way to configure it through Java Config.

<mongo:auditing />

<mongo:mongo id="mongo" />

<bean class="">
    <constructor-arg name="mongo" ref="mongo" />
    <constructor-arg name="databaseName" value="blog-tests" />


Configuration above provides us way for auditing that includes versioning and timestamps. Example document will look like:

public class Item {
    private String id;


    private Long version;
    private DateTime createdAt;
    private DateTime lastModified;


Now you can save document using MongoTemplate or your repository and all annotated fields are automagically set.

As you have probably noticed I did not use here user related annotations @CreatedBy and @LastModifiedBy. In order to use them we need to tell Spring who is a current user.

First add user related fields to your audited class:

private String createdBy;

private String lastModifiedBy;

Then create your implementation of AuditorAware that will obtain current user (probably from session or Spring Security context – depends on your application):

public class MyAppAuditor implements AuditorAware<String> {

    public String getCurrentAuditor() {
        // get your user name here
        return "John Doe";

Last thing is to tell Spring Data MongoDB about this auditor aware class by little modification in Mongo configuration:

<mongo:auditing auditor-aware-ref="auditor" />
<bean id="auditor" class=""/>


Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply

× two = 10

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: