org.springframework.beans.factory.BeanCreationException 同时尝试将我的 mongodb 云数据库连接到我的 spring 应用程序

org.springframework.beans.factory.BeanCreationException while try to connect my mongodb cloud data base to my spring application

我正在尝试创建我的基本 spring 应用程序并尝试连接到我的 MongoDB 数据库。 所以创建我的实体

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

@Document
    @Data @AllArgsConstructor @NoArgsConstructor @ToString
    public class MyProduct {
    @Id
    private String id;
    private String name;
    private int price;
}

我的资料库

import com.nizar.back.demo.entities.MyProduct;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource
public interface MyProductRepository extends MongoRepository<MyProduct,String> {
}

我的application.properties

server.port=8093
spring.data.mongodb.uri=mongodb://<my username>:<my password>@cluster0.zgrrb.mongodb.net/appDatabase?retryWrites=true&w=majority

这是我的申请class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;

@SpringBootApplication
@EnableAutoConfiguration(exclude={ DataSourceAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
public class DemoApplication {

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

}

当我尝试 运行 我的应用程序时,我注意到我有这个错误,我不明白为什么?

2021-07-21 18:04:49.921  INFO 7524 --- [           main] com.nizar.back.demo.DemoApplication      : Starting DemoApplication using Java 1.8.0_291 on DESKTOP-6PB4U74 with PID 7524 (C:\dev projects\spring project\demo\target\classes started by abdel in C:\dev projects\spring project\demo)
2021-07-21 18:04:49.924  INFO 7524 --- [           main] com.nizar.back.demo.DemoApplication      : No active profile set, falling back to default profiles: default
2021-07-21 18:04:50.411  INFO 7524 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2021-07-21 18:04:50.459  INFO 7524 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 43 ms. Found 1 MongoDB repository interfaces.
2021-07-21 18:04:50.724  WARN 7524 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myProductRepository' defined in com.nizar.back.demo.dao.MyProductRepository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' available
2021-07-21 18:04:50.732  INFO 7524 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-07-21 18:04:50.749 ERROR 7524 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

A component required a bean named 'mongoTemplate' that could not be found.

我刚刚做这个项目是为了了解 spring 引导。我只需要找出为什么会出现此错误以及如何解决它

它没有选择正确的驱动程序版本。 所以我将 https://cloud.mongodb.com/ 上的驱动器版本更改为 3.4 或更高版本,它给我另一个 url 连接并且它工作

mongodb://<MyUsername>:<Mypassword>@cluster0-shard-00-00.zgrrb.mongodb.net:27017,cluster0-shard-00-01.zgrrb.mongodb.net:27017,cluster0-shard-00-02.zgrrb.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=atlas-sr24zl-shard-0&authSource=admin&retryWrites=true&w=majority