【问题标题】:I am trying to push data into my db using JpaRepository but there was some error stopping me to do so我正在尝试使用 JpaRepository 将数据推送到我的数据库中,但是有一些错误阻止我这样做
【发布时间】:2020-04-17 07:00:02
【问题描述】:
    *************************** APPLICATION FAILED TO START ***************************

Description:

Field userRepository in com.Lex.Exercise.Service.RegistrationService required a bean of type 'com.Lex.Exercise.Repository.UserRepository' 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.Lex.Exercise.Repository.UserRepository' in your configuration.

//用户存储库

package com.Lex.Exercise.Repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.Lex.Exercise.Model.UserEntity;

  public interface UserRepository extends JpaRepository<UserEntity, String>{

  }

//注册服务

@ComponentScan(basePackages = "com.Lex.Exercise.Repository")
@Component
public class RegistrationService {  
@Autowired
private UserRepository userRepository;
//business methods and other validations comes here
}

这是主类

    package com.Lex.Exercise.SpringBootDemo;
@SpringBootApplication
@PropertySource(value = { "classpath:configuration.properties" })
public class SpringBootDemoApplication implements CommandLineRunner {

    @Autowired
    private RegistrationService service;
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }
}

以下是项目结构: /SpringBootDemo/src/main/java/com/Lex/Exercise/Model/User.java /SpringBootDemo/src/main/java/com/Lex/Exercise/Model/UserEntity.java /SpringBootDemo/src/main/java/com/Lex/Exercise/Repository/UserRepository.java /SpringBootDemo/src/main/java/com/Lex/Exercise/Service/RegistrationService.java /SpringBootDemo/src/main/java/com/Lex/Exercise/SpringBootDemo/SpringBootDemoApplication.java /SpringBootDemo/src/main/resources/application.properties /SpringBootDemo/src/main/resources/configuration.properties

请帮我解决这个问题

【问题讨论】:

  • 请提供您的项目结构
  • /SpringBootDemo/src/main/java/com/Lex/Exercise/Model/User.java /SpringBootDemo/src/main/java/com/Lex/Exercise/Model/UserEntity.java /SpringBootDemo /src/main/java/com/Lex/Exercise/Repository/UserRepository.java /SpringBootDemo/src/main/java/com/Lex/Exercise/Service/RegistrationService.java /SpringBootDemo/src/main/java/com/Lex /Exercise/SpringBootDemo/SpringBootDemoApplication.java /SpringBootDemo/src/main/resources/application.properties /SpringBootDemo/src/main/resources/configuration.properties SpringBootDemoApplication.java为主类

标签: spring-boot spring-data-jpa


【解决方案1】:

删除您的RegistrationService.class 上的@ComponentScan(scanBasePackages = "com.Lex.Exercise.Repository")

你的SpringBootDemoApplication.class 应该是这样的。比com.Lex.Exercise 子包中的组件将被注入到应用程序上下文中。

@SpringBootApplication
@ComponentScan(basePackages = {"com.Lex.Exercise"})
public class SpringBootDemoApplication{
    public static void main(String[] args) {
        new SpringApplication(SpringBootDemoApplication.class).run(args);
    }
}

一般来说,将 SpringApplication 放在顶级包中是个好主意(就像你的情况 com.Lex.Exercise),因为 Spring Boot 会自动扫描该类的所有子包。所以你不需要额外的@ComponentScan

【讨论】:

  • 非常感谢。现在,当我将主类移动到顶级包 com.Lex.Exercise 时,它​​工作正常。
  • @RANADHEERSANNIHITH 如果这个答案对您有任何帮助,您应该支持它。如果它解决了您的问题,请接受它。这将帮助其他人看到 a) 问题已得到解答,b) 确定重要的,即有用的答案。
【解决方案2】:

@ComponentScan(basePackages = "com.Lex.Exercise.Repository") 属于您的配置,可能是您的应用程序类

【讨论】:

  • 你的意思是 @ComponentScan(basePackages = "com.Lex.Exercise.Repository") 应该在我的主课中提及?
  • 是的。至少这是我的第一个猜测。如果这不起作用,我们可能需要有关如何设置应用程序的更多信息(pom.xml,主应用程序类)
  • 更新了帖子本身的主类和项目结构。请检查并帮助@Jens Schauder
猜你喜欢
  • 2022-01-15
  • 2021-08-28
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
相关资源
最近更新 更多