【问题标题】:mongoTemplate bean could not be found找不到 mongoTemplate bean
【发布时间】:2017-09-23 15:50:24
【问题描述】:

我正在使用 mvc + mongodb 构建一个非常简单的 Spring Boot 应用程序。我使用 Spring 初始化程序创建具有 web、thymeleaf 和 mongo 依赖项的项目。我有一个控制器、一个模型和一个视图,但在尝试执行应用程序时不断出现错误:

Description:

Field repo in com.example.CustomerController required a bean named 'mongoTemplate' that could not be found.


Action:

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

客户控制器:

import model.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by Hello on 25/04/2017.
 */

@Controller
@RequestMapping("/home")
public class CustomerController {

    @Autowired
    CustomerMongoRepo repo;


    @RequestMapping(value = "/home", method= RequestMethod.GET)
    public String viewingHome(Model model){
        //initDB();
        model.addAttribute("key", "THIS IS FROM THE MODEL");

        return "homepage";
    }

}

客户MongoRepo:

    import org.springframework.data.repository.CrudRepository;

        import model.Customer;

public interface CustomerMongoRepo extends CrudRepository {}

主应用程序:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
public class DemoApplication extends WebMvcAutoConfiguration {


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

客户模型:

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * Created by Hello on 25/04/2017.
 */

@Document(collection = "customerCollection")
public class Customer {
    @Id
    private int id;
    private String fName;
    private String sName;

    public Customer(){}

    public Customer(int id, String fName, String sName){
        setfName(fName);
        setsName(sName);
        setId(id);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getsName() {
        return sName;
    }

    public void setsName(String sName) {
        this.sName = sName;
    }


}

我的应用程序属性:

spring.data.mongodb.database=customer
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.uri=mongodb://localhost:27018/mydb
spring.data.mongo.repositories.enabled=true

【问题讨论】:

  • 如果有效,你能接受答案吗?

标签: java mongodb spring-mvc spring-boot


【解决方案1】:

您正在排除 Mongo 配置。

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})

那么 spring 将如何为您创建 mongoTemplate。删除此排除项或手动创建 MongoTemplate 并将其注册到应用程序上下文(使用 @Bean

【讨论】:

    【解决方案2】:

    我最近遇到了同样的问题,我的解决方案是删除spring-data-mongodb

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

    我保留了spring-boot-starter-data-mongodb:

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

    可以看出,要么两者一起产生冲突,要么我需要添加我不知道的“东西”。

    快乐的代码!我希望对你有所帮助


    更新

    后来我发现了一些与问题可能描述的内容相关的内容,尽管我永远无法完成检查: https://stackoverflow.com/a/12389842/7911776

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      相关资源
      最近更新 更多