【问题标题】:Cannot autowire MongoOperations bean无法自动装配 MongoOperations bean
【发布时间】:2020-10-22 19:23:43
【问题描述】:

我想创建一个 mongo capped 集合,为此,我在教程中看到我需要使用 MongoOperations bean。但我不能自动接线。

说明:

com.daimon.reactivespring.initialize.ItemDataInitializer 中构造函数的参数 1 需要一个无法找到的 'org.springframework.data.mongodb.core.MongoOperations' 类型的 bean。

行动:

考虑在你的配置中定义一个“org.springframework.data.mongodb.core.MongoOperations”类型的bean。

build.gradle:

plugins {
    id 'org.springframework.boot' version '2.3.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'com.daimon.reactivespring'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'de.flapdoodle.embed:de.flapdoodle.embed.mongo'
    testImplementation 'io.projectreactor:reactor-test'
}

test {
    useJUnitPlatform()
    exclude 'com/daimon/reactivespring/fluxmono/**'
}

我需要注入的类:

@Component
@RequiredArgsConstructor
@Profile("!test")
public class ItemDataInitializer implements CommandLineRunner {

    //@Autowired
    private final ItemReactiveRepository itemReactiveRepository;
    //@Autowired
    private final MongoOperations mongoOperations;

    @Override
    public void run(String... args) throws Exception {
        initialDatSetup();
        createCappedCollection();
    }

    private void initialDatSetup() {
        itemReactiveRepository.deleteAll()
                .thenMany(Flux.fromIterable(initItems()))
                .flatMap(itemReactiveRepository::save)
                .subscribe(item -> System.out.println("Item inserted " + item));
    }

    private List<Item> initItems() {
        return Arrays.asList(new Item(null, "Samsung TV", 200.0),
                new Item(null, "Apple TV", 300.0), new Item(null, "LG TV", 400.0));
    }

    private void createCappedCollection() {
        mongoOperations.dropCollection(ItemCapped.class);
        mongoOperations.createCollection(ItemCapped.class, CollectionOptions.empty().maxDocuments(20).size(50000).capped());
    }
}

谢谢

【问题讨论】:

  • 你正在混合反应和阻塞的东西。没有反应性MongoOperations。您将不得不使用反应式ReactiveMongoOperations
  • 你能告诉我们你的spring boot主类,mongodb应用程序属性吗?看看你的类路径,你的类路径上有 spring-boot-autoconfigure 吗?

标签: java spring mongodb spring-boot gradle


【解决方案1】:

有人建议切换到 ReactiveMongoOperations。但由于某种原因,它不会创建上限集合。我切换到 spring boot 2.2.7.RELEASE 版本并且它工作了

【讨论】:

【解决方案2】:

尝试添加这个构造函数:

    public ItemDataInitializer(ItemReactiveRepository itemReactiveRepository, MongoOperations mongoOperations){
           this.itemReactiveRepository = itemReactiveRepository;
           this.mongoOperations = mongoOperations;
    }

【讨论】:

    猜你喜欢
    • 2016-09-27
    • 2015-07-05
    • 2012-12-04
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多