General Auditing Configuration for MongoDB

To activate auditing functionality, add the Spring Data Mongo auditing namespace element to your configuration, as the following example shows:

Example 1. Activating auditing by using XML configuration
<mongo:auditing mapping-context-ref="customMappingContext" auditor-aware-ref="yourAuditorAwareImpl"/>

Since Spring Data MongoDB 1.4, auditing can be enabled by annotating a configuration class with the @EnableMongoAuditing annotation, as the followign example shows:

Example 2. Activating auditing using JavaConfig
@Configuration
@EnableMongoAuditing
class Config {

  @Bean
  public AuditorAware<AuditableUser> myAuditorProvider() {
      return new AuditorAwareImpl();
  }
}

If you expose a bean of type AuditorAware to the ApplicationContext, the auditing infrastructure picks it up automatically and uses it to determine the current user to be set on domain types. If you have multiple implementations registered in the ApplicationContext, you can select the one to be used by explicitly setting the auditorAwareRef attribute of @EnableMongoAuditing.