Search code examples
configurationspring-data-jpaauditing

Spring Data JPA programmatically enable auditing


How can I do the same as: <jpa:auditing auditor-aware-ref="yourAuditorAwarebean" /> programmatically in a @Configuration class?
Is the spring xml configuration mandatory to do this?

reference: http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE/reference/html/jpa.repositories.html#d0e2427


Solution

  • Java Config for auditing is now supported since Spring Data JPA 1.5 (link to documentation)

    Replace the <jpa:auditing auditor-aware-ref="yourAuditorAwarebean" /> with the @EnableJpaAuditing annotation on any of your configuration class.

    E.g:

    @Configuration
    @EnableJpaAuditing
    class Config {
    
      @Bean
      public AuditorAware<AuditableUser> yourAuditorAwarebean() {
        return new YourAuditorAwareImpl();
      }
    }