【发布时间】:2020-11-08 21:11:50
【问题描述】:
我在启动我的 Spring-boot 项目时遇到问题。
界面:
import com.sample.rest.entity.Customer;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
@Repository
public interface CustomerRepositoryInterface extends CrudRepository<Customer, Integer> {
}
休息控制器类
@RestController
@RequestMapping("/api/customers")
public class CustomerRestController {
@Autowired
private CustomerRepositoryInterface custRepo;
@GetMapping
public Iterable<Customer> getAllCustomers(){
return custRepo.findAll();
}
}
应用类:
@SpringBootApplication(scanBasePackages = "com.sample.rest")
public class SpringbootRestApiApplication {
public static void main(String[] args)
{
SpringApplication.run(SpringbootRestApiApplication.class, args);
}
}
构建进行得很好,但是在运行 spring-boot 应用程序时,我收到以下错误。
控制台:
*任务:bootRun [2020-07-19T19:46:13.013Z] [org.springframework.context.support.AbstractApplicationContext] [restartedMain] [559] [WARN] 上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory。 UnsatisfiedDependencyException:创建名称为“customerRestController”的 bean 时出错:通过字段“custRepo”表示不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.sample.rest.utility.CustomerRepositoryInterface”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 线程“task-2”java.lang.IllegalStateException 中的异常:EntityManagerFactory 已关闭 在 org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:509) 在 org.hibernate.internal.SessionFactoryImpl.getProperties(SessionFactoryImpl.java:503) 在 org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.findDataSource(DataSourceInitializedPublisher.java:105) 在 org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.publishEventIfRequired(DataSourceInitializedPublisher.java:97) 在 org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.access $100(DataSourceInitializedPublisher.java:50) 在 org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher$DataSourceSchemaCreatedPublisher.lambda$postProcessEntityManagerFactory$0(DataSourceInitializedPublisher.java:200) 在 java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) 在 java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) 在 java.base/java.lang.Thread.run(Thread.java:834) [2020-07-19T19:46:13.013Z] [org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter] [restartedMain] [40] [错误] ****************************** 应用程序无法启动 ****************************** 描述:com.sample.rest.controller.CustomerRestController 中的字段 custRepo 需要一个 bean 输入“com.sample.rest.utility.CustomerRepositoryInterface”,可以 找不到。注入点有如下注解:
- @org.springframework.beans.factory.annotation.Autowired(required=true) 行动:考虑定义一个类型的bean 'com.sample.rest.utility.CustomerRepositoryInterface' 在您的 配置。*
任何人的任何想法/解决方案都会有所帮助。
【问题讨论】:
-
在主类里面打什么招呼
-
@silentsudo 它只是一个评论......你可以忽略它。最初我添加它是为了检查结构是否正常工作。但是当我添加接口并自动连接它时,它开始出现我提到的问题并且无法启动。
标签: spring-boot rest