Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行Spring Boot应用程序时获取与bean创建相关的错误_Java_Spring Boot_Jpa_Spring Data Jpa - Fatal编程技术网

Java 运行Spring Boot应用程序时获取与bean创建相关的错误

Java 运行Spring Boot应用程序时获取与bean创建相关的错误,java,spring-boot,jpa,spring-data-jpa,Java,Spring Boot,Jpa,Spring Data Jpa,我正在用JPA学习spring boot,在启动我的应用程序时遇到了一个问题。有人能帮我解决这个问题吗?错误是- Description: Field userRepo in com.example.demo.controller.DemoController required a bean of type 'com.example.demo.repo.UserRepo' that could not be found. The injection point has the following

我正在用JPA学习spring boot,在启动我的应用程序时遇到了一个问题。有人能帮我解决这个问题吗?错误是-

Description: Field userRepo in com.example.demo.controller.DemoController required a bean of type 'com.example.demo.repo.UserRepo' 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.example.demo.repo.UserRepo' in your configuration.
控制器

public class DemoController {
private static final String CLASS_NAME = DemoController.class.getName();
@Autowired
private UserRepo userRepo;

@Autowired
private UserServiceImpl userService;

  @PostMapping(value = "/hello", consumes = "application/json", produces = "application/json") 
  public String createUser(@RequestBody User user) {
       long count = userRepo.count();
      return "Done"; 
  }
}
存储库

@Repository

public interface UserRepo extends JpaRepository<User, Integer> {    
}
项目结构


我用@repository和控制器上的@Autowired注释了存储库。我哪里做错了

您的配置类中可能缺少
@EnableJpaRepositories
注释。然后,这将扫描带有
@Repository
注释的所有类。

您有许多不需要的注释

根据文件——

我们通常建议您将主应用程序类定位在根包中,高于其他类。

我怀疑组件扫描无法扫描您的UserRepo

删除所有不必要的注释

@ComponentScan("com.example.demo*")
@EnableJpaRepositories(basePackages = {"com.example.demo*"})
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

您添加了很多实际上并不需要的注释

进行以下更改

     DemoController.java -> Add `@Controller or @RestController` annotation
     Application.java --> Remove annotations completely

    @ComponentScan() 
    @EnableJpaRepositories()
    @EnableAutoConfiguration()
让我知道它是否有效


试试。

我正在使用@EnableJpaRepositories,我已经编辑了我的问题并添加了应用程序类。你能试着从包名中删除星号吗?这是没有必要的,我也不确定这是否会导致问题,因为我觉得这段代码没问题。
DemoController
是否正确地用
@Controller
注释了?你好,Ashutosh,我已经编辑了我的问题并添加了包结构。你能删除所有的@ComponentScan(“com.example.demo*)吗@EnableJpaRepositories(basePackages={“com.example.demo*”})@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}),因为我认为它们不是必需的。请确保用@RestController注释控制器。我按照您的建议删除了所有内容,现在工作正常。谢谢但是,您能解释一下为什么从应用程序类中删除所有注释后,它会正常工作吗?
     DemoController.java -> Add `@Controller or @RestController` annotation
     Application.java --> Remove annotations completely

    @ComponentScan() 
    @EnableJpaRepositories()
    @EnableAutoConfiguration()