Description:
Parameter 0 of constructor in com.example.service.QuestionService required a bean of type 'com.example.Dao.QuestionDao' that could not be found.
Action:
Consider defining a bean of type 'com.example.Dao.QuestionDao' in your configuration.
I couldn't solve this problem for last 2 days ..........If anyone know please help me...
`Controller:
@RestController
@RequestMapping("question")
public class QuestionController<Question> {
private QuestionSerice questionService;
public QuestionController (QuestionService questionService)
{ this.questionService = questionService;
}
// ------Read Operation----
@GetMapping("allQuestions")
public List<com.example.model.Question> getAllQuestions () {
return questionService.getAllQuestions();
}
@GetMapping("category/{category}")
public List<com.example.model.Question> getQuestionsByCategory (@PathVariable String category) {
return questionService.getQuestionsByCategory( category);
}
//------Create Operation---
@PostMapping("create")
public String createQuestion (@RequestBody Question question) {
return questionService.createQuestion ((com.example.model.Question) question);
}
}
QuestionDao:
@Repository
public interface QuestionDao extends JpaRepository<Question, Integer>{
List<Question> findByCategory (String category);
}
Service:
@Service
public class QuestionService {
private QuestionDao questionDao;
public QuestionService(QuestionDao questionDao) {
this.questionDao = questionDao;
}
public List<Question> getAllQuestions() {
return questionDao.findAll();
}
public String createQuestion(Question question) {
questionDao.save(question) ;
return "Sucess";
}
public List<Question> getQuestionsByCategory(String category) {
return questionDao.findByCategory(category);
}
}
Dao: @Repository public interface QuestionDao extends JpaRepository<Question,Integer>{
List<Question> findByCategory(String category);
} model:
@Data
@Entity
@Table(name = "question")
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "questionTitle")
private String questionTitle;
@Column(name = "option1")
private String option1;
@Column(name = "option2")
private String option2;
@Column(name = "option3")
private String option3;
@Column(name = "option4")
private String option4;
@Column(name = "rightAnswer")
private String rightAnswer;
@Column(name = "difficultylevel")
private String difficultylevel;
@Column(name = "category")
private String category;
}
main:
@SpringBootApplication
@ComponentScan(basePackages={"com.example.controller","com.example.demo","com.example.Dao","com.example.model","com.example.service"})
public class QuizMaker1Application {
private QuestionDao questionDao;
public static void main(String[] args) {
SpringApplication.run(QuizMaker1Application.class, args);
}
}
First of all remove ComponentScan
annotation from QuizMaker1Application
.
It is not required for spring boot application to find beans in root directory.
Then remove private QuestionDao questionDao;
from QuizMaker1Application
. It's not used there, so it's not required.
This should be fine to start and find beans and repository.
If it will not work. Try adding @EnableJpaRepositories("com.example.dao")
to QuizMaker1Application
.