我一直在尝试创建CRUD应用程序,但是当我尝试运行该应用程序时,出现错误“ Field repo in com.nationwide.individualproject.controllers.BoulderController required a bean of type 'com.nationwide.individualproject.Repos.BoulderRepo' that could not be found
”。以及标题中的消息。
我尝试添加各种注释,例如@ComponantScan
和@EntityScan
,但这并没有改变任何内容。该应用程序由位于com.nationwide.individualproject; .controllers, .data, .Repos
下的三个包组成,每个类中的每个类分别标记为@RestController, @Entity, and @Repository
。
主应用程序类位于同一级别的所有这些软件包的外部。
主应用程序如下:
package com.nationwide.individualproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication()
public class IndividualProjectApplication {
public static void main(String[] args) {
SpringApplication.run(IndividualProjectApplication.class, args);
}
}
BoulderRepo看起来像:
package com.nationwide.individualproject.Repos;
import com.nationwide.individualproject.data.Boulder;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.Date;
@Repository
public interface BoulderRepo extends JpaRepository<Boulder, Integer> {
...
}
Boulder类如下:
package com.nationwide.individualproject.data;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Date;
@Entity
public class Boulder {
@Id
...
}
最后,控制器看起来像:
package com.nationwide.individualproject.controllers;
import com.nationwide.individualproject.Repos.BoulderRepo;
import com.nationwide.individualproject.data.Boulder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Date;
@RestController
public class BoulderController {
@Autowired
private BoulderRepo repo;
...
}
看看与我类似的其他问题,他们认为这与将类全部放在不同的包中有关,但是我仍然不确定为什么当主类与所有包处于同一级别时会出现错误
项目结构的外观如下所示:
---com.nationwide.individualproject
---controllers
---data
---Repos
---individualProjectApplication
编辑更长的Stacktrace消息
2019-10-21 14:04:21.705 INFO 3172 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-10-21 14:04:21.716 WARN 3172 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'boulderController': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.nationwide.individualproject.Repos.BoulderRepo' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'speedRepo': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'boulderRepo': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'topRopeRepo': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'leadRepo': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
2019-10-21 14:04:21.717 INFO 3172 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-10-21 14:04:21.720 INFO 3172 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2019-10-21 14:04:21.732 INFO 3172 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
2019-10-21 14:04:21.735 INFO 3172 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2019-10-21 14:04:21.746 INFO 3172 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-10-21 14:04:21.849 ERROR 3172 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field repo in com.nationwide.individualproject.controllers.BoulderController required a bean of type 'com.nationwide.individualproject.Repos.BoulderRepo' 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.nationwide.individualproject.Repos.BoulderRepo' in your configuration.
答案 0 :(得分:0)
您的pom.xml文件中存在版本问题 从下面的代码中删除版本。
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
应该像
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>