在spring boot中使用autowire时出现以下错误
***************************
APPLICATION FAILED TO START
***************************
Description:
Field candidateRepository in io.xgeek.interviewercalendarapi.service.CandidateServiceImpl required a bean of type 'io.xgeek.interviewercalendarapi.repository.CandidateRepository' 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 'entityManagerFactory' in your configuration.
自定义,来自ServiceImpl类,它调用控制器
@Service
public class CandidateServiceImpl implements CandidateService{
@Autowired
private CandidateRepository candidateRepository;
// ... interface methods here...
我的存储库接口:
@Repository
public interface CandidateRepository extends JpaRepository <Candidate, Long> {
}
服务接口:
public interface CandidateService {
Candidate saveCandidate(Candidate candidate);
List<Candidate> fetchCandidateList();
}
主类:'
@SpringBootApplication
public class InterviewerCalendarApiApplication {
public static void main(String[] args) {
SpringApplication.run(InterviewerCalendarApiApplication.class, args);
}
}
我的pom.xml依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>
我的项目结构
我什么都试过了。这些类的顺序肯定是正确的,它们位于spring引导应用程序main的子包中。@ComponentScan包不能工作。扫描包不能工作。所有的符号似乎都是对的。这类问题我都找遍了,可就是找不到解决办法。
我已经有一段时间没有使用Spring Data了,但是web建议您需要修改您的服务类以包含@PersistenceContext
注释。
@Service
public class CandidateServiceImpl implements CandidateService {
@PersistenceContext
private EntityManager entityManager;
...
示例:如何在Spring Boot中获取JPA EntityManager