没有名为“mongoTemplate”的 bean 可用。 Spring Boot + MongoDB

Posted

技术标签:

【中文标题】没有名为“mongoTemplate”的 bean 可用。 Spring Boot + MongoDB【英文标题】:No bean named 'mongoTemplate' available. Spring Boot + MongoDB 【发布时间】:2020-12-02 18:23:31 【问题描述】:

我正在使用 Spring Boot 和 MongoDB 构建后端,首先我正在创建用户存储库、服务和控制器,并收到此错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userRepository in com.package.package.controller.UserController required a bean named 'mongoTemplate' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean named 'mongoTemplate' in your configuration.

代码如下:

用户控制器:

package com.package.package.controller;

import com.package.package.entities.User;
import com.package.package.repositories.UserRepository;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController 
    @Autowired
    private UserRepository userRepository;

    @GetMapping(value = "/")
    public List<User> getAllUsers() 
        return userRepository.findAll();
    

    @GetMapping(value = "/id")
    public User getUserById(@PathVariable("id") ObjectId id) 
        return userRepository.findBy_id(id);
    

    @PutMapping(value = "/id")
    public void modifyUserById(@PathVariable("id") ObjectId id, @Valid @RequestBody User user) 
        user.set_id(id);
        userRepository.save(user);
    

    @PostMapping(value = "/")
    public User createUser(@Valid @RequestBody User user) 
        user.set_id(ObjectId.get());
        userRepository.save(user);
        return user;
    

    @DeleteMapping(value = "/id")
    public void deleteUser(@PathVariable ObjectId id) 
        userRepository.delete(userRepository.findBy_id(id));
    


用户存储库:

package com.package.package.repositories;

import com.package.package.entities.User;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends MongoRepository<User, String> 
    User findBy_id(ObjectId _id);


用户服务:

package com.package.package.service;

import com.package.package.entities.User;
import org.bson.types.ObjectId;

import java.util.List;

public interface UserService 
    User save(User user);
    User findbyid(ObjectId _id);
    List<User> getAll();
    void delete(User user);


主应用程序:

package com.package.package;

import com.package.package.repositories.UserRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = UserRepository.class)
public class TestApplication 

    public static void main(String[] args) 
        SpringApplication.run(TestApplication.class, args);
    



我使用我过去的项目作为参考(不同之处在于这是使用 postgres 完成的)。 你想要的任何其他信息,请告诉我。 提前致谢。

更新: 这是 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wazzka</groupId>
    <artifactId>wazzka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>wazzka</name>
    <description>WazzkaProject</description>

    <properties>
        <java.version>14</java.version>
        <spring.framework.version>5.2.8.RELEASE</spring.framework.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>3.0.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Lovelace-SR9</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>jakarta.validation</groupId>
            <artifactId>jakarta.validation-api</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>spring-milestone</id>
            <name>Spring Maven MILESTONE Repository</name>
            <url>https://repo.spring.io/libs-milestone</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

您是否加入了spring-boot-starter-mongodb?我建议使用过去的项目作为reference,但使用start.spring.io 生成新项目。 解决了这个问题,但现在我遇到了这个问题:justpaste.it/8ermn。现在帖子有 pom.xml,以防万一。 除非绝对需要,否则不要在使用 Boot 时指定版本。您明确地 (1) 导入 Spring Data Lovelace 列车(这很好),(2) 为 Commons 和 JPA 指定 2.2.3,以及 (3) 为 MongoDB 指定 3.0.3。如果遇到不兼容问题,这不足为奇。 【参考方案1】:

尝试添加此依赖项:

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

【讨论】:

以上是关于没有名为“mongoTemplate”的 bean 可用。 Spring Boot + MongoDB的主要内容,如果未能解决你的问题,请参考以下文章

mongodb-mongotemplate进行地理坐标操作

没有名为 authenticationManager 的 bean [重复]

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“transactionManager”的bean

没有名为“myJobPerformable”的 bean 可用

NoSuchBeanDefinitionException:没有名为“entityManagerFactory”的bean可用

没有定义名为“PlaceDAO”的 bean