【问题标题】:SpringBoot: Failed to Start : Field <field Name >in RestController required a bean of typeSpringBoot:启动失败:RestController 中的字段 <字段名称> 需要类型为 bean
【发布时间】: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(D​​ataSourceInitializedPublisher.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


【解决方案1】:

已解决: 找到问题了,是项目结构的问题。

以前我的“SpringbootRestApiApplication.java”文件位于“src/main/java/com/simple/rest/api”pkg 下。

我只是把它放在“src/main/java/com/simple/rest”下,因为我的其他java文件在“src/main/java/com/simple/rest/*”下

所以我把文件放在父 pkg 下。它得到了解决。 现在我也不需要再指定“scanBasePackages”了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2018-09-22
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 2020-05-01
    • 2013-02-12
    相关资源
    最近更新 更多