简体   繁体   中英

Cannot autowire MongoOperations bean

I want to create a mongo capped collection, for this, I saw in tutorials that I need to use MongoOperations bean. But I cannot autowire it.

Description:

Parameter 1 of constructor in com.daimon.reactivespring.initialize.ItemDataInitializer required a bean of type 'org.springframework.data.mongodb.core.MongoOperations' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.data.mongodb.core.MongoOperations' in your configuration.

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/**'
}

the class where I need to inject it:

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

Thank you

There was a suggestion to switch to ReactiveMongoOperations. But for some reason it doesn't create a capped collection. I switched to spring boot 2.2.7.RELEASE version and it worked

try adding this constructor:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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