gpt4 book ai didi

java - 无法 Autowiring MongoOperations bean

转载 作者:行者123 更新时间:2023-12-03 05:23:24 28 4
gpt4 key购买 nike

我想创建一个 mongo capped 集合,为此,我在教程中看到我需要使用 MongoOperations bean。但我无法 Autowiring 它。
描述:
com.daimon.reactivespring.initialize.ItemDataInitializer 中构造函数的参数 1 需要找不到类型为“org.springframework.data.mongodb.core.MongoOperations”的 bean。
行动:
考虑在你的配置中定义一个 'org.springframework.data.mongodb.core.MongoOperations' 类型的 bean。
构建.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/**'
}
我需要注入(inject)它的类:
@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 boot 2.2.7.RELEASE 版本并且它工作

关于java - 无法 Autowiring MongoOperations bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62692199/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com