与 mongoTemplate bean Spring boot 相关的UnsatisfiedDependencyException

问题描述 投票:0回答:1

我面临这个错误:

Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'
我的 Spring Boot 应用程序不知道原因或该怎么做,我在一个应用程序中同时使用 mySql 和 mongoDb,我之前已经这样做过,并且它在另一个应用程序中的工作方式相同应用程序没有问题,我不知道该怎么处理它或为什么显示此错误,通常这是我的代码:

应用程序道具:

spring.application.name=Inventory
spring.datasource.url=jdbc:mysql://localhost:3306/inventory
spring.datasource.username=root
spring.datasource.password=
spring.data.mongodb.uri=mongodb://localhost:27017/
spring.data.mongodb.database=mongoInventory
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
logging.level.org.springframework.data.mongodb=DEBUG
logging.level.org.springframework=DEBUG
server.error.include-exception=true
server.error.include-stacktrace=always
spring.main.allow-circular-references=true
logging.level.org.hibernate=TRACE

摇篮构建:

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.2.5'
    id 'io.spring.dependency-management' version '1.1.4'
}

group = 'com.InventoryCHU'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '21'
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
    implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
    implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'
    implementation group: 'org.python', name: 'jython-slim', version: '2.7.3'
    compileOnly 'org.projectlombok:lombok'
    implementation 'com.h2database:h2'
    implementation 'io.jsonwebtoken:jjwt:0.9.1' // JWT library
    runtimeOnly 'com.mysql:mysql-connector-j'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'


}

tasks.named('test') {
    useJUnitPlatform()
}

我所有的代表都是这样的:

package com.InventoryCHU.Inventory.Repository;

import com.InventoryCHU.Inventory.Models.BreakDown;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.Optional;

public interface BreakDownRepository extends MongoRepository<BreakDown , String> {
    Optional<BreakDown> findBreakDownByItemId(String item);
}

服务:

package com.InventoryCHU.Inventory.Services;

import com.InventoryCHU.Inventory.Models.BreakDown;
import com.InventoryCHU.Inventory.Repository.BreakDownRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class BreakDownService {
    @Autowired
    BreakDownRepository breakDownRepository;

    public List<BreakDown> getAllBreakDowns(){
        return breakDownRepository.findAll();
    }

    public Optional<BreakDown> getBreakDownById(String Id){
        return breakDownRepository.findById(Id);
    }

    public BreakDown createBreakDown(BreakDown breakDown){
        return breakDownRepository.save(breakDown);
    }

    public void deleteBreakDown(String Id){
        breakDownRepository.deleteById(Id);
    }

    public Optional<BreakDown> findBreakDownByItemId(String productId){ return breakDownRepository.findBreakDownByItemId(productId); }

}

控制器:

package com.InventoryCHU.Inventory.Controllers;

import com.InventoryCHU.Inventory.Models.BreakDown;
import com.InventoryCHU.Inventory.Services.BreakDownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/breakdown")
public class BreakDownController {
    @Autowired
    BreakDownService breakDownService;

    @GetMapping
    public ResponseEntity<List<BreakDown>> getAllBreakDowns() {
        List<BreakDown> breakDowns = breakDownService.getAllBreakDowns();
        return ResponseEntity.ok(breakDowns);
    }

    @GetMapping("/{id}")
    public ResponseEntity<BreakDown> getBreakDownById(@PathVariable String id) {
        Optional<BreakDown> breakDown = breakDownService.getBreakDownById(id);
        return breakDown.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/product-{id}")
    public ResponseEntity<BreakDown> getBreakDownByUserId(@PathVariable("id") String productId){
        Optional<BreakDown> breakDown = breakDownService.findBreakDownByItemId(productId);
        return breakDown.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PostMapping(consumes = "application/json", produces = "application/json")
    public ResponseEntity<BreakDown> createBreakDown(@RequestBody BreakDown breakDown) {
        BreakDown createdBreakDown = breakDownService.createBreakDown(breakDown);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdBreakDown);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Void> deleteBreakDown(@PathVariable String id) {
        breakDownService.deleteBreakDown(id);
        return ResponseEntity.noContent().build();
    }

}

对于显示它太长的错误,所以我认为最好只显示这个:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'breakDownController': Unsatisfied dependency expressed through field 'breakDownService': Error creating bean with name 'breakDownService': Unsatisfied dependency expressed through field 'breakDownRepository': Error creating bean with name 'breakDownRepository' defined in com.InventoryCHU.Inventory.Repository.BreakDownRepository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'breakDownService': Unsatisfied dependency expressed through field 'breakDownRepository': Error creating bean with name 'breakDownRepository' defined in com.InventoryCHU.Inventory.Repository.BreakDownRepository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'breakDownRepository' defined in com.InventoryCHU.Inventory.Repository.BreakDownRepository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]: Unsatisfied dependency expressed through method 'mongoTemplate' parameter 1: Error creating bean with name 'mappingMongoConverter' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]: Unsatisfied dependency expressed through method 'mappingMongoConverter' parameter 1: Error creating bean with name 'mongoMappingContext' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.class]: Unable to make field private java.util.Date java.security.Timestamp.timestamp accessible: module java.base does not "opens java.security" to unnamed module @3c9754d8
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mappingMongoConverter' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]: Unsatisfied dependency expressed through method 'mappingMongoConverter' parameter 1: Error creating bean with name 'mongoMappingContext' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.class]: Unable to make field private java.util.Date java.security.Timestamp.timestamp accessible: module java.base does not "opens java.security" to unnamed module @3c9754d8
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoMappingContext' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.class]: Unable to make field private java.util.Date java.security.Timestamp.timestamp accessible: module java.base does not "opens java.security" to unnamed module @3c9754d8
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private java.util.Date java.security.Timestamp.timestamp accessible: module java.base does not "opens java.security" to unnamed module @3c9754d8``

我不知道到底要尝试什么,我查看了谷歌,但没有找到任何相关信息,我也尝试了gpt,它说的是java和mongodb之间的不兼容版本以及与mongoTemplate bean相关的UnsatisfiedDependencyException,没有提供任何工作解决方案,如果有任何解决方案的想法,我会很高兴

java mongodb spring-boot dependencies
1个回答
0
投票

我尝试了很多事情,发现该错误与将列表添加到 mongodb 模型有关,当使用简单对象作为字符串和整数时,一切正常,但是当您声明对象列表时,发生了这种情况,我做了一些研究,我发现Spring Boot 3.3.2 版本上的 mongo Db 实现存在一些问题

© www.soinside.com 2019 - 2024. All rights reserved.