I am getting this error: `
***************************
APPLICATION FAILED TO START
***************************
Description:
Field questionDao in com.surbhi.quizapp.service.QuestionService required a bean of type 'com.surbhi.quizapp.dao.QuestionDao' 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 of type 'com.surbhi.quizapp.dao.QuestionDao' in your configuration.
` I have the following file structure and files.
File structure: https://i.sstatic.net/8jSrf.png
QuestionService.java
@Service
public class QuestionService {
QuestionDao questionDao;
public QuestionService(QuestionDao questionDao) {
super();
this.questionDao = questionDao;
}
public ResponseEntity<List<Question>> getAllQuestions() {
try {
return new ResponseEntity<>(questionDao.findAll(), HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
}
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.BAD_REQUEST);
}
public ResponseEntity<List<Question>> getQuestionsByCategory(
String category
) {
try {
return new ResponseEntity<>(
questionDao.findByCategory(category),
HttpStatus.OK
);
} catch (Exception e) {
e.printStackTrace();
}
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.BAD_REQUEST);
}
public ResponseEntity<String> addQuestion(Question question) {
questionDao.save(question);
return new ResponseEntity<>("success", HttpStatus.CREATED);
}
}
QuestionDao.java
@Repository
public interface QuestionDao extends JpaRepository<Question, Integer> {
List<Question> findByCategory(String category);
@Query(
value = "SELECT * FROM question q Where q.category=:category ORDER BY RANDOM() LIMIT :numQ",
nativeQuery = true
)
List<Question> findRandomQuestionsByCategory(String category, int numQ);
}
I have also tried removing @Autowire and creating constructor but then that also didn't work and gave this error
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.surbhi.quizapp.service.QuestionService required a bean of type 'com.surbhi.quizapp.dao.QuestionDao' that could not be found.
Action:
Consider defining a bean of type 'com.surbhi.quizapp.dao.QuestionDao' in your configuration.
What should I do, Also added
@SpringBootApplication(scanBasePackages={ "com.surbhi.quizapp.controller", "com.surbhi.quizapp.dao","com.surbhi.quizapp.model","com.surbhi.quizapp.service"})
in main, but that doesn't work either.
Used @ComponentScan, doesn't work
Main class file:
@SpringBootApplication
@ComponentScan("com.surbhi.quizapp.dao")
public class QuizappApplication {
public static void main(String[] args) {
SpringApplication.run(QuizappApplication.class, args);
}
}
Looking at the package photo you uploaded, it appears that there are two QuizappApplication
classes. (com.surbhi.quizapp
, com.surbhi.quizapp.service
)
Will the same problem occur if I remove the QuizappApplication
class that exists in the com.surbhi.quizapp.service
package and write it as follows?
@SpringBootApplication
public class QuizappApplication {
public static void main(String[] args) {
SpringApplication.run(QuizappApplication.class, args);
}
}
Additionally, the @Repository annotation is not required on the QuestionDao interface.