繁体   English   中英

无法自动装配 MongoOperations bean

[英]Cannot autowire MongoOperations bean

我想创建一个 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/**'
}

class 我需要在其中注入它:

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

谢谢

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

尝试添加此构造函数:

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

暂无
暂无

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

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