couldn't resolve the issue "Consider defining a bean named 'mongoTemplate' in your configuration." in spring data

Viewed 292

I am building a simple spring boot application to save some data into Mongo. I am stuck with the issue "Consider defining a bean named 'mongoTemplate' in your configuration.". I tried many things. But nothing works. Below is the code.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.2</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ex</groupId>
<artifactId>mongo-export</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mongo-export</name>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

Main App

//@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
//@SpringBootApplication(exclude = {MongoAutoConfiguration.class, 
//MongoDataAutoConfiguration.class})
@EnableMongoRepositories(basePackageClasses = StudentsRepository.class)
@SpringBootApplication
@ComponentScan("com.ex")
public class MongoExportApplication implements CommandLineRunner {

@Autowired
StudentsRepository studentsRepository;

public static void main(String[] args) {
    SpringApplication.run(MongoExportApplication.class, args);
}

@Override
public void run(String... args) {
    studentsRepository.save(new Student(UUID.randomUUID(), 123, "Student1"));
    System.out.println("~~~~~~~~~~~~~Saved the student object~~~~~~~~~~~~~");
}
}

Student Bean

@Document("students")
public class Student {
@Id
private UUID id;

private int rollNumber;

private String name;

public Student(UUID id, int rollNumber, String name) {
    this.id = id;
    this.rollNumber = rollNumber;
    this.name = name;
}

public UUID getId() {
    return id;
}

public void setId(UUID id) {
    this.id = id;
}

public int getRollNumber() {
    return rollNumber;
}

public void setRollNumber(int rollNumber) {
    this.rollNumber = rollNumber;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

Repository

 public interface StudentsRepository extends MongoRepository<Student, UUID> {
 }

application.properties

spring.data.mongodb.uri=mongodb+srv://username:pwd.url/dataBaseName?retryWrites=true&w=majority
  • I used the actual username, password, url, databaseName in the above value

Trace

***************************
APPLICATION FAILED TO START
***************************

Description:

Field studentsRepository in     
com.ex.mongoexport.MongoExportApplication required a bean named 
'mongoTemplate' that could not be found.

The injection point has the following annotations:
- 
  @org.springframework.beans.factory.annotation.Autowired(required=true)  

Action:

Consider defining a bean named 'mongoTemplate' in your configuration.
Process finished with exit code 1
0 Answers
Related