连接Spring Boot与MongoDB使用Java类。

huangapple go评论80阅读模式
英文:

connect spring boot with mongodb using java class

问题

我有这个结构

项目 (module.pom)
- 连接 (module.jar)
    - src/main/java
        - com.mycompany.connection
            - connectionBD.class
    - ...
    - pom.xml
- 人员 (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

我的 connectionBD.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 类是这样的。

@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());
        }
    }
}

当我运行项目时,我收到一个错误,说我的数据库连接有问题,我不知道问题在哪里。
有人可以帮帮我吗?


错误信息

嵌套异常是 org.springframework.beans.factory.BeanCreationException: 在类路径资源 [com/mycompany/connection/connectionBD.class] 中定义的名为 'mongoTemplate' 的 bean 创建失败。通过工厂方法实例化 bean 时出错;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.mongodb.core.MongoTemplate]:工厂方法 'mongoTemplate' 抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/mycompany/connection/connectionBD.class] 中定义的名为 'mongoDatabaseFactory' 的 bean 创建失败。通过工厂方法实例化 bean 时出错;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.mongodb.MongoDatabaseFactory]:工厂方法 'mongoDatabaseFactory' 抛出异常;嵌套异常是 java.lang.IllegalArgumentException:连接字符串无效。连接字符串必须以 'mongodb://' 或 'mongodb+srv://' 开头。

英文:

I have this structure

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

my connectionBD.class class is this.

@Configuration
public class connectionBD {

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

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

and my PersonApplication.class class that is in another module.

@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());
	    }
    }
}

When I run the project, I get an error saying that my connection to my database is wrong and I don't know what the problem is.
Somebody could help me.

Error message

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://

答案1

得分: 1

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

你需要编辑。将 mongo 改为 mongodb SimpleMongoClientDatabaseFactory

@Configuration
public class connectionBD {

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

> Connection strings must start with either 'mongodb://' or 'mongodb+srv://

You need to edit. mongo -> mongodb SimpleMongoClientDatabaseFactory

@Configuration
public class connectionBD {

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

huangapple
  • 本文由 发表于 2020年6月29日 05:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/62628461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定