I am making a very silly mistake but not able to figure out how to fix.
I have a simple SpringBoot app using profiles, which connect to MongoDb.
My pom.xml dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
My StudentController.java
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(method = RequestMethod.GET)
public Collection<Student> getAllStudents(){
return studentService.getAllStudents();
}
}
My StudentService.java
@Service
public class StudentService {
@Autowired
private StudentDao studentDao;
public Collection<Student> getAllStudents(){
return this.studentDao.getAllStudents();
}
}
My StudentDao.java interface:
public interface StudentDao {
Collection<Student> getAllStudents();
}
My MongoStudentDaoImpl.java:
@Repository
@Profile("test")
public class MongoStudentDaoImpl implements StudentDao {
@Autowired
private MongoStudentRepo repo;
@Override
public Collection<Student> getAllStudents() {
return repo.findAll();
}
}
My MongoStudentRepo.java:
@Profile("test")
public interface MongoStudentRepo extends MongoRepository<Student, String> {
}
When I am trying to start the application using the "test" profile, here is the error I am seeing:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'studentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentService': Unsatisfied dependency expressed through field 'studentDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoStudentDaoImpl': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'MongoStudentRepo' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
What am I missing in here? Do I need to add an annotation to MongoStudentRepo.java
?
Thanks in advance.