简体   繁体   English

() 中的字段 personRepository 需要一个 () 类型的 bean,但找不到

[英]Field personRepositary in () required a bean of type () that could not be found

I am trying to run a spring developed web app and I'm getting the following error.我正在尝试运行 spring 开发的 web 应用程序,但出现以下错误。

在此处输入图像描述

My folder structure is as follows.我的文件夹结构如下。

在此处输入图像描述

Here is my PersonRepositary.java code which is inside the repositary folder.这是我的 PersonRepository.java 代码,它位于存储库文件夹中。

package com.travelx.travelx.repositary;

import org.springframework.data.repository.CrudRepository;
import com.travelx.travelx.models.Person;

public interface PersonRepositary extends CrudRepository<Person, Integer> {

}

The RegisterController.java file which is in the controllers folder is ac follows.控制器文件夹中的 RegisterController.java 文件如下所示。

package com.travelx.travelx.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.travelx.travelx.models.Person;
import com.travelx.travelx.repositary.PersonRepositary;

@RestController
@RequestMapping("register")
public class RegisterController {

    @Autowired
    private PersonRepositary personRepositary;

    @PostMapping("login")
    public String registerPerson(@RequestBody Person person) {
        personRepositary.save(person);
        return "You are Registered!";
    }

}

And the TravelXApplication.java file which is in the controllers is below.下面是控制器中的 TravelXApplication.java 文件。

package com.travelx.travelx.controllers;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@ComponentScan
@EntityScan
@EnableJpaRepositories
public class TravelxApplication {

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

}

I'm trying to make a web page where a person can register to a site.我正在尝试制作一个 web 页面,人们可以在其中注册到站点。 Here, I'm using xampp as my platform to handle the back end.在这里,我使用 xampp 作为我的平台来处理后端。 As shown in the image, the controllers, repositories and and models are implemented in separate folders.如图所示,控制器、存储库和模型在单独的文件夹中实现。 I'm new to Spring.我是 Spring 的新手。 So no matter how hard I to find what the problem is, I cant seem to find it.因此,无论我多么难以找到问题所在,我似乎都无法找到它。 Can some one help me please?有人能帮助我吗?

--------------UPDATE------------------ - - - - - - - 更新 - - - - - - - - -

I've moved my TravelXApplication.java to the com.travelx.travelx and now this error is gone.Spring works fine.我已将 TravelXApplication.java 移至 com.travelx.travelx,现在此错误消失了。Spring 工作正常。 However when I open my form, insert data and try to save it, the browser gives me the following error.但是,当我打开表单、插入数据并尝试保存时,浏览器会给我以下错误。

在此处输入图像描述

How do I solve it?我该如何解决?

Your PersonRepositary is not registered as a bean in your Spring context.您的PersonRepositary未在 Spring 上下文中注册为bean In practice, this means that Spring is not be able to inject it in your RegisterController .实际上,这意味着 Spring 无法将其注入您的RegisterController

I suspect that @EnableJpaRepositories , @EntityScan and @ComponentScan are unnecessary in your main application class and are actually causing Spring automatic configuration to be overridden.我怀疑@EnableJpaRepositories@EntityScan@ComponentScan在您的主应用程序 class 中是不必要的,实际上导致 Spring 自动配置被覆盖。 Try deleting these three annotations from TravelxApplication .尝试从TravelxApplication中删除这三个注释。 Here's the answer to why it should still work without annotations.这是为什么它在没有注释的情况下仍然可以工作的答案

Update: just noticed that your TravelxApplication is located in the controllers package, but then it won't have visibility to your repository.更新:刚刚注意到您的TravelxApplication位于控制器package 中,但是它不会看到您的存储库。 Make sure to move your main class to the com.travelx.travelx package.确保将您的主要 class 移动到com.travelx.travelx package。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Controller.Controller 中的错误字段存储库需要找不到类型为“Controller.Repository”的 bean - Error Field Repository in Controller.Controller required a bean of type 'Controller.Repository' that could not be found B类中构造函数的参数0需要一个找不到A类类型的Bean - Parameter 0 of constructor in Class B required a bean of type Class A that could not be found com.abc.serive.MysqlService 中的字段 countRepo 需要一个无法找到的“com.abc.repository.ClicksQuickReplyRepository”类型的 bean - Field countRepo in com.abc.serive.MysqlService required a bean of type 'com.abc.repository.ClicksQuickReplyRepository' that could not be found Spring Boot MySQL REST错误-找不到名为&#39;entityManagerFactory&#39;/&#39;emf&#39;的bean - Spring Boot MySQL REST Error - Required a bean named 'entityManagerFactory'/'emf' that could not be found 十进制的数据字段类型无法按要求工作 - data field type for decimal does not work as required 找到Scala Spark类型不匹配的单位,必需为rdd.RDD - Scala Spark type missmatch found Unit, required rdd.RDD 找不到类型或命名空间名称 &#39;MySQL&#39; VS2017 - Type or namespace name 'MySQL' could not be found VS2017 错误 CS0246:找不到类型或命名空间名称“MySql” - error CS0246: The type or namespace name 'MySql' could not be found 如何解决此“必需的bean”错误 - How to resolve this “required a bean” error AppMaker记录保存失败:“ id”字段值为必填项,但发现为“ null”。 (自动递增) - AppMaker Record save failed: 'id' field value is required, but found 'null'. (auto increment)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM