Reputation: 1003
I am trying to enable auto audit fields with spring data mongodb as explained here. Below is my configuration class
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.abc")
@EnableMongoRepositories(basePackages = "com.abc.xyz.repository")
@EnableMongoAuditing
public class ApplicationConfiguration {
@Bean
public MongoDbFactory mongoDbFactory() throws Exception {
ServerAddress serverAddress = new ServerAddress("127.0.0.1", 27017);
MongoCredential mongoCredential = MongoCredential.createCredential("user", "test", "abc123".toCharArray());
MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(mongoCredential));
return new SimpleMongoDbFactory(mongoClient, "test");
}
@Bean
public MongoTemplate mongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory());
}
}
But when I add @EnableMongoAuditing, I am getting the below error on starting the server.
Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoAuditingHandler': Cannot create inner bean '(inner bean)#6dca0c34' of type [org.springframework.data.mongodb.config.MongoAuditingRegistrar$MongoMappingContextLookup] while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#6dca0c34': Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [org.springframework.data.mongodb.core.convert.MappingMongoConverter] found for dependency [org.springframework.data.mongodb.core.convert.MappingMongoConverter]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.mongodb.core.convert.MappingMongoConverter] found for dependency [org.springframework.data.mongodb.core.convert.MappingMongoConverter]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:236)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
Upvotes: 7
Views: 17074
Reputation: 1802
1 : Make sure you have spring-data-mongodb
2 : if you are using @CreatedDate
or @LastModifiedDate
then you don't need any additional configuration.
class ClassName {
.......
@CreatedDate
private DateTime createdDate;
@LastModifiedDate
private DateTime @lastModifiedDate;
}
3: if you are using @CreatedBy
and @LastModifiedBy
then you have to implement AuditorAware<T>
SPI interface
class ClassName {
.......
@CreatedBy
private String createdBy;
@LastModifiedBy
private String lastModifiedBy;
}
public class AppAuditor implements AuditorAware<String> {
@Override
public String getCurrentAuditor() {
// get your user name here
return "xxxx";
}
}
Implementation of AuditorAware based on Spring Security from spring doc
class SpringSecurityAuditorAware implements AuditorAware<User> {
public User getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated()) {
return null;
}
return ((MyUserDetails) authentication.getPrincipal()).getUser();
}
}
Upvotes: 20
Reputation: 29276
Can you check if you have Spring Data MongoDB
dependency 1.9.4.RELEASE
or above as mongoAuditingHandler
requires MappingMongoConverter which is available in version 1.9.4.RELEASE
or above as per changelog - spring-data-mongodb-changelog, for example:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.9.4.RELEASE</version>
</dependency>
Upvotes: 4