Springboot - 注入点有以下注释: - @org.springframework.beans.factory.annotation.Autowired(required=true)

问题描述 投票:0回答:1

我收到此错误: `

***************************
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.

` 我有以下文件结构和文件。

文件结构: https://i.stack.imgur.com/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);
}

我也尝试删除 @Autowire 并创建构造函数,但那也不起作用并给出了此错误

***************************
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.

我该怎么办,也补充一下

@SpringBootApplication(scanBasePackages={ “com.surbhi.quizapp.controller”,“com.surbhi.quizapp.dao”,“com.surbhi.quizapp.model”,“com.surbhi.quizapp.service”})

主要,但这也不起作用。

使用了@ComponentScan,不起作用

主类文件:

@SpringBootApplication

@ComponentScan("com.surbhi.quizapp.dao")

public class QuizappApplication {

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

}
java spring spring-boot jpa dao
1个回答
0
投票

看你上传的包裹照片,似乎有两个

QuizappApplication
类。 (
com.surbhi.quizapp
,
com.surbhi.quizapp.service
)

如果我删除

QuizappApplication
包中存在的
com.surbhi.quizapp.service
类并按如下方式编写,会出现同样的问题吗?

@SpringBootApplication
public class QuizappApplication {

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

}

此外,QuestionDao接口上不需要@Repository注解。

© www.soinside.com 2019 - 2024. All rights reserved.