[英]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.