Auditing

自 Spring Data MongoDB 1.4 以来,可以通过使用 @EnableMongoAuditing 注释注释配置类来启用审计功能,如下例所示:

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

  • Imperative

  • Reactive

  • XML

@Configuration
@EnableMongoAuditing
class Config {

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

  @Bean
  public ReactiveAuditorAware<AuditableUser> myAuditorProvider() {
      return new ReactiveAuditorAwareImpl();
  }
}
<mongo:auditing mapping-context-ref="customMappingContext" auditor-aware-ref="yourAuditorAwareImpl"/>

如果您将类型为 AuditorAware / ReactiveAuditorAware 的 bean 暴露到 ApplicationContext 中,审计基础架构将自动选取它并使用它来确定要设置到域类型上的当前用户。如果您在 ApplicationContext 中注册了多个实现,您可以通过明确设置 @EnableMongoAuditingauditorAwareRef 属性来选择要使用的实现。

If you expose a bean of type AuditorAware / ReactiveAuditorAware 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.