简体   繁体   English

运行 Spring 启动应用程序时出现与 bean 创建相关的错误

[英]Getting error related with bean creation while run Spring Boot app

I am learning spring boot with JPA and i have a problem when launch my app.我正在使用 JPA 学习 spring 引导,并且在启动我的应用程序时遇到问题。 Could somebody help me to solve this issue?有人可以帮我解决这个问题吗? Error is -错误是 -

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.

Controller Controller

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存储库

@Repository

public interface UserRepo extends JpaRepository<User, Integer> {    
}

Application class应用 class

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

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

}

Project structure项目结构

在此处输入图像描述

I annotated repository with @Repository and using @Autowired on controller.我使用@Repository 注释存储库并在controller 上使用@Autowired。 Where i am doing wrong?我在哪里做错了?

You added quite a lot annotations which aren't really needed.您添加了很多实际上并不需要的annotations

Make the following changes进行以下更改

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

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

Let me know if it works.让我知道它是否有效。

Try.尝试。

You are probably missing the @EnableJpaRepositories annotation in your configuration class.您可能在配置 class 中缺少@EnableJpaRepositories注释。 This will then scan for all classes with the @Repository annotation.然后,这将扫描所有带有@Repository注释的类。

Can you share the package structure of your project?你能分享一下你项目的package结构吗?

I doubt that component scan is unable to scan your UserRepo class.我怀疑组件扫描无法扫描您的UserRepo class。

暂无
暂无

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

相关问题 尝试使用 dynamodb 和 graphql 运行我的 Spring Boot 应用程序时,我不断收到此 @bean 错误 - I keep getting this @bean error when trying to run my spring boot app with dynamodb and graphql 在 Spring Boot 应用程序中 bean 验证失败时没有得到正确的响应 - Not getting proper response while bean validation failed in spring boot app Spring Boot-Bean创建异常 - Spring Boot - Bean creation exception 在Spring Boot中创建Bean的顺序 - Order of bean creation in Spring Boot Spring bean创建错误 - Spring bean creation error 启动 spring 启动应用程序时 bean 创建错误 - bean creation error when starting spring boot application 尝试运行Spring Boot应用程序时出错。 创建名称为bean的错误 - Error trying to run Spring boot app. Error creating bean with name 通过 Postman 运行 Spring 启动应用程序时出错 - Getting error while running Spring boot app via Postman 在Weblogic 12.2.1上部署Spring Boot 2应用程序时,创建名称为“ httpPutFormContentFilter”的bean时出错 - Error creating bean with name 'httpPutFormContentFilter' while deploying spring boot 2 app on weblogic 12.2.1 "<i>Spring Boot @Mapper Bean creation issue : Application Failed to start.<\/i> Spring Boot @Mapper Bean 创建问题:应用程序无法启动。<\/b> <i>Error : Consider defining a bean of Type<\/i>错误:考虑定义一个 bean 类型<\/b>" - Spring Boot @Mapper Bean creation issue : Application Failed to start. Error : Consider defining a bean of Type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM