java在使用mongotemplate时遇到ClassNotFound异常
运行使用mongo模板的spring引导应用程序时,我遇到以下异常:
Caused by: java.lang.ClassNotFoundException: org.springframework.data.mongodb.MongoDatabaseFactory
这是主要代码:
package com.example.redismongo.mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.config.EnableMongoAuditing;
import org.springframework.data.mongodb.core.MongoTemplate;
@Configuration
@EnableMongoAuditing
public class MongoConfiguration {
@Bean
public MongoClient mongo(){
return MongoClients.create("mongodb://localhost:27017");
}
@Primary
@Bean(name = "mongoTemplate")
public MongoTemplate mongoTemplate(){
return new MongoTemplate(mongo(), "test");
}
}
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation group: 'org.springframework.data', name: 'spring-data-commons', version: '2.2.8.RELEASE'
implementation group: 'org.springframework.data', name: 'spring-data-mongodb', version: '2.2.8.RELEASE'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
我需要测试2.2.8版本,因为这是正在工作中使用的版本。我试图在我的电脑上测试它的一些功能,以测试某些失败的情况。如何使2.2.8正常工作
# 1 楼答案
尝试添加spring启动程序数据mongodb而不是spring数据mongodb