繁体   English   中英

使用 java ZA2F2ED4F8EBC2CBB4C21A29DC40AB6 将 spring 引导与 mongodb 连接

[英]connect spring boot with mongodb using java class

我有这个结构

project  (module.pom)
- connection                     (module.jar)
      - src/main/java
            -com.mycompany.connection
                -connectionBD.class
      - ...
      - pom.xml
- person                         (module.jar)
      - src/main/java
            - com.mycompany.person
                -personApplication.class
            - com.mycompany.person.controller
                -...
            - com.mycompany.person.model
                -...
      - src/main/resources
      - ...
      - pom.xml
pom.xml

我的连接BD.class class 是这个。

@Configuration
public class connectionBD {

    @Bean
    public MongoDatabaseFactory mongoDatabaseFactory(){
        return new SimpleMongoClientDatabaseFactory("mongo://localhost:27017/lydsam");
    }

    @Bean
    public MongoTemplate mongoTemplate() {
        return new MongoTemplate(mongoDatabaseFactory());
    }
}

我的PersonApplication.class class 在另一个模块中。

@SpringBootApplication(scanBasePackages = { "com.mycompany.person", "com.mycompany.connection" })
public class PersonApplication {

    public static void main(String[] args) {
        try {
            SpringApplication.run(PersonApplication.class, args);
        } catch (Exception e) {
            System.out.println("Error: "+ e.getMessage());
        }
    }
}

当我运行该项目时,我收到一条错误消息,提示我与数据库的连接错误,我不知道问题出在哪里。 有人可以帮助我。

错误信息

nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [com/mycompany/connection/connectionBD.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDatabaseFactory' defined in class path resource [com/mycompany/connection/connectionBD.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.MongoDatabaseFactory]: Factory method 'mongoDatabaseFactory' threw exception; nested exception is java.lang.IllegalArgumentException: The connection string is invalid. Connection strings must start with either 'mongodb://' or 'mongodb+srv://

连接字符串必须以 'mongodb://' 或 'mongodb+srv://' 开头

你需要编辑。 mongo -> mongodb SimpleMongoClientDatabaseFactory

@Configuration
public class connectionBD {

    @Bean
    public MongoDatabaseFactory mongoDatabaseFactory(){
        return new SimpleMongoClientDatabaseFactory("mongodb://localhost:27017/lydsam");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM