package org.javers.spring.example; import com.github.fakemongo.Fongo; import com.google.common.collect.ImmutableMap; import com.mongodb.client.MongoDatabase; import org.javers.core.Javers; import org.javers.core.JaversBuilder; import org.javers.repository.mongo.MongoRepository; import org.javers.spring.auditable.AuthorProvider; import org.javers.spring.auditable.CommitPropertiesProvider; import org.javers.spring.auditable.SpringSecurityAuthorProvider; import org.javers.spring.auditable.aspect.JaversAuditableAspect; import org.javers.spring.auditable.aspect.springdata.JaversSpringDataAuditableRepositoryAspect; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import java.util.Map; /** * @author bartosz walacik */ @Configuration @ComponentScan(basePackages = "org.javers.spring.repository.mongo") @EnableAspectJAutoProxy @EnableMongoRepositories(basePackages = "org.javers.spring.repository.mongo") public class JaversSpringMongoApplicationConfig { /** * Creates JaVers instance backed by {@link MongoRepository} */ @Bean public Javers javers() { MongoRepository javersMongoRepository = new MongoRepository(mongoDB()); return JaversBuilder.javers() .registerJaversRepository(javersMongoRepository) .build(); } /** * MongoDB setup */ @Bean public MongoDatabase mongoDB() { return new Fongo("test").getDatabase("test"); } /** * Enables auto-audit aspect for ordinary repositories.<br/> * * Use {@link org.javers.spring.annotation.JaversAuditable} * to mark data writing methods that you want to audit. */ @Bean public JaversAuditableAspect javersAuditableAspect() { return new JaversAuditableAspect(javers(), authorProvider(), commitPropertiesProvider()); } /** * Enables auto-audit aspect for Spring Data repositories. <br/> * * Use {@link org.javers.spring.annotation.JaversSpringDataAuditable} * to annotate CrudRepositories you want to audit. */ @Bean public JaversSpringDataAuditableRepositoryAspect javersSpringDataAuditableAspect() { return new JaversSpringDataAuditableRepositoryAspect(javers(), authorProvider(), commitPropertiesProvider()); } /** * Required by auto-audit aspect. <br/><br/> * * Creates {@link SpringSecurityAuthorProvider} instance, * suitable when using Spring Security */ @Bean public AuthorProvider authorProvider() { return new SpringSecurityAuthorProvider(); } /** * Optional for auto-audit aspect. <br/> * @see CommitPropertiesProvider */ @Bean public CommitPropertiesProvider commitPropertiesProvider() { return new CommitPropertiesProvider() { @Override public Map<String, String> provide() { return ImmutableMap.of("key", "ok"); } }; } }