0

我使用所有持续版本(修复其他问题)

mongobee 0.13

fongo 2.2.0-RC3-SNAPSHOT

mongodb驱动程序3.8.2

当我运行我的应用程序时,出现此错误:

org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/myproject/company/configuration/MongoConfiguration.class] 中定义名称为“mongobeeStandalone”的 bean 创建错误:调用 init 方法失败;嵌套异常是 java.lang.AbstractMethodError: com.mongodb.client.internal.FongoOperationExecutor.execute(Lcom/mongodb/operation/ReadOperation;Lcom/mongodb/ReadPreference;Lcom/mongodb/ReadConcern;)Ljava/lang/Object; ...引起:java.lang.AbstractMethodError:com.mongodb.client.internal.FongoOperationExecutor.execute(Lcom/mongodb/operation/ReadOperation;Lcom/mongodb/ReadPreference;Lcom/mongodb/ReadConcern;)Ljava/lang/Object ;

我的课:

@Bean
public Mongobee mongobeeStandalone(final MongoTemplate mongoTemplate, final MongoClient mongoClient) {
    final Mongobee runner = new Mongobee(mongoClient);
    runner.setChangeLogsScanPackage(Changelog1.class.getPackage().getName()); // package to scan for changesets
    runner.setDbName(mongoTemplate.getDb().getName());
    return runner;
}

@Bean
public MongoClient mongoClient(final MongoDbFactory mongoDbFactory) {
    final Mongo mongo = mongoDbFactory.getLegacyDb().getMongo();
    if (!MongoClient.class.isInstance(mongo)) {
        throw new UnsupportedOperationException("Must be a MongoClient");
    }
    return MongoClient.class.cast(mongo);
}

编辑

我的测试配置:

@Configuration
public class FoncgoConfiguration extends AbstractMongoConfiguration {

    @Bean
    public Fongo fongo() {
        return new Fongo("mongo-test");
    }

    @Override
    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        final SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(mongoClient(), getDatabaseName());
        final MongoTemplate mongoTemplate = new MongoTemplate(simpleMongoDbFactory, mappingMongoConverter());
        return mongoTemplate;
    }

    @Override
    @Bean
    public MongoClient mongoClient() {
        return MockMongoClient.create(fongo());
    }

}
4

1 回答 1

0

您应该使用 Spring Data Mongo,它将创建和注入com.mongodb.MongoClientbean。所以删除mongoClient()方法并添加 Mongodb Spring Data 依赖项:

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

Spring Boot 还提供了一个MongoTemplatebean 并自动配置它。对于测试,您应该使用带有@DataMongoTest注释的Embedded Mongo,因为它配置了一个. 有关测试示例,请参见此处MongoTemplate

于 2018-10-18T09:47:33.133 回答