gpt4 book ai didi

Java Spring Boot 应用程序无法检测到 Mongodb 存储库

转载 作者:太空宇宙 更新时间:2023-11-04 09:26:28 25 4
gpt4 key购买 nike

我正在尝试在 Spring Boot 应用程序中的 MongoRepository 上使用我的 mongodb 文档来实现具有持久性的 Spring 状态机。

问题是,当我在 Spring 应用程序类中 Autowiring MongoRepository 类时,它说:

Caused by: java.lang.NoClassDefFoundError: org/springframework/data/mongodb/repository/MongoRepository
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:802)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:700)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.getDeclaredFields0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredFields(Class.java:3067)
at java.base/java.lang.Class.getDeclaredFields(Class.java:2254)
at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:755)
... 20 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.data.mongodb.repository.MongoRepository
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 33 common frames omitted

非常感谢您提前提供的帮助:)

pom.xml 文件:

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.statemachine</groupId>
<artifactId>spring-statemachine-data-mongodb</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.1.10.RELEASE</version>
</dependency>
<小时/>

自定义 MongoRepository:

package com.itu.celikelni.spring.boot.statemachine.data;

import com.itu.celikelni.spring.boot.statemachine.data.PaymentDbObject;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface PaymentRepository extends MongoRepository<PaymentDbObject, Integer> {

}
<小时/>

申请

package com.itu.celikelni.spring.boot.statemachine;
import com.itu.celikelni.spring.boot.statemachine.entity.Events;
import com.itu.celikelni.spring.boot.statemachine.entity.States;
import com.itu.celikelni.spring.boot.statemachine.entity.Payment;
import com.itu.celikelni.spring.boot.statemachine.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.persist.StateMachinePersister;
import com.itu.celikelni.spring.boot.statemachine.data.PaymentRepository;
import com.itu.celikelni.spring.boot.statemachine.data.PaymentDbObject;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@ComponentScan("com.itu.celikelni.spring.boot.statemachine.data")
@EnableMongoRepositories("com.itu.celikelni.spring.boot.statemachine.data")
@SpringBootApplication
public class Application implements CommandLineRunner {

@Autowired
private StateMachine<States, Events> stateMachine;

@Autowired
private StateMachinePersister<States, Events, Integer> stateMachinePersister;

@Autowired
private PaymentService paymentService;

@Autowired
private PaymentRepository paymentRepository;

最佳答案

让 spring boot mongo starter 控制你的 spring data mongo 依赖版本

所以从 pom.xml 中删除 spring-data-mongodb 的工件版本

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>

关于Java Spring Boot 应用程序无法检测到 Mongodb 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57645175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com