Springboot версии 2.2.1 при переходе с 2.1.0
Причина: java .lang.NullPointerException в org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException (MongoTemplate. java: 2902) в org.springframework.data.mongodb.core.MultiTenancyMongoTemplate.execute (MultiTenancyMongoTemplate. java: 81) в org.springframework.data.mongodb.core.MultiTenancyMongoTemplate. 54) по адресу org.springframework.data.mongodb.core.DefaultIndexOperations.execute (DefaultIndexOperations. java: 214) по адресу org.springframework.data.mongodb.core.DefaultIndexOperations.ensureIndex (DefaultIndexOperations *: 121 или 101) в * 1014 в * 1014 в * 1014 в *. .springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex (. MongoPersistentEntityIndexCreator java: 158) в org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes (MongoPersistentEntityIndexCreator java:. 148) в орг .springframework.data.m ongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes (MongoPersistentEntityIndexCreator. java: 130) в org.springframework.data.mongodb.core.index.MongoPersistingEntity .core.index. . (MultiTenancyMongoTemplate. java: 33) в ApplicationsMongoTemplate. (ApplicationsMongoTemplate. java: 21) в MongoConfiguration.mongoOperations (MongoConfiguration. java: 53) в MongoConfiguration at MongoConfiguration urationClassEnhancer $ BeanMethodInterceptor.intercept (ConfigurationClassEnhancer. java: 363) в MongoConfiguration .base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl. java: 62) в java .base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke * 1030): .base / java .lang.reflect.Method.invoke (Метод. java: 566) в org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate (SimpleInstantiationStrategy. java: 154) ... 122 больше
@Configuration
@EnableMongoRepositories(basePackages = "repositories", mongoTemplateRef = "mongoOperations")
public class MongoConfiguration {
private final MongoClientOptions options;
private final MongoClientFactory factory;
public MongoConfiguration(MongoProperties properties,
ObjectProvider<MongoClientOptions> options, Environment environment) {
this.options = options.getIfAvailable();
this.factory = new MongoClientFactory(properties, environment);
}
@Bean(destroyMethod = "close")
public MongoClient mongo() {
return this.factory.createMongoClient(this.options);
}
@Bean
public ValidatingMongoEventListener validatingMongoEventListener() {
return new ValidatingMongoEventListener(validator());
}
@Bean
public LocalValidatorFactoryBean validator() {
return new LocalValidatorFactoryBean();
}
@Bean
public MongoOperations mongoOperations(final ShareContextMongoDbFactory mongoDbFactory, final MongoConverter mongoConverter) {
return new ApplicationsMongoTemplate(mongoDbFactory, mongoConverter);
}
@Bean
public ShareContextMongoDbFactory mongoDbFactory(final MongoClient mongoClient, final MongoProperties mongoProperties) {
return new ShareContextMongoDbFactory(mongoClient, mongoProperties.getDatabase());
}
}
public class ApplicationsMongoTemplate extends MultiTenancyMongoTemplate implements
ApplicationEventPublisherAware {
private ApplicationEventPublisher applicationEventPublisher;
public ApplicationsMongoTemplate(
ShareContextMongoDbFactory mongoDbFactory,
MongoConverter mongoConverter) {
super(mongoDbFactory, mongoConverter);
}
/**
* Remove the given object from the collection by id.
*
* @param object must not be {@literal null}.
* @return the {@link DeleteResult} which lets you access the results of the previous delete.
*/
@Override
@NonNull
public DeleteResult remove(Object object) {
Assert.notNull(object, "Object must not be null!");
applicationEventPublisher.publishEvent(new BeforeDeleteEvent(object));
return super.remove(object);
}
@Override
public void setApplicationEventPublisher(
@NonNull ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
}
build.gradle dependecies
dependencies {
compile "org.springframework.cloud:spring-cloud-starter-openfeign"
compile "org.springframework.boot:spring-boot-starter-cache"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-starter-data-mongodb"
compile "org.springframework.boot:spring-boot-starter-security"
compile "org.springframework.boot:spring-boot-starter-validation"
compile "org.springframework.boot:spring-boot-starter-websocket"
compile "org.springframework.boot:spring-boot-starter-web"
compile "org.springframework.boot:spring-boot-starter-aop"
compile "org.springframework.boot:spring-boot-starter-amqp"
compileOnly "org.springframework.boot:spring-boot-configuration-processor"
testCompile "org.springframework.boot:spring-boot-starter-test"
testCompile "org.openjdk.jmh:jmh-core:${jmh_version}"
testCompile "org.openjdk.jmh:jmh-generator-annprocess:${jmh_version}"
testCompile "org.powermock:powermock-module-junit4:${powermock_version}"
testCompile "org.powermock:powermock-api-mockito2:${powermock_version}"
testCompile "org.mockito:mockito-core:2.18.3"
testCompile "commons-beanutils:commons-beanutils:1.9.4"
}