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

Viewed 1348

I wanted to ask why does this code throw error with 2.3.0.RELEASE and not with 2.0.6.RELEASE of Spring Data MongoDB:

@Service
public class ServiceX {
    @Autowired private MongoTemplate mongoTemplate;
    @Scheduled(fixedDelay = 1000) public void scheduled() {
        // I am using mongoTemplate here
    }
}
1 Answers

MongoTemplate is an imperative variant. You should use ReactiveMongoTemplate instead since you are on Reactive MongoDB. It used to work in older versions but it won't work with Spring Boot 2.3.0.RELASE and above.

Infact, I created this issue yesterday:

You can always switch to non reactive variant of Spring Data MongoDB using spring-boot-starter-data-mongodb instead of spring-boot-starter-data-mongodb-reactive.

Related