【问题标题】:Spring boot CrudRepo Define a beanSpring boot CrudRepo 定义一个bean
【发布时间】:2017-04-24 05:56:15
【问题描述】:

我在 Spring Boot 中遇到了关于存储库的问题。 我有一个服务:

@Service("userService")
public class UserServiceImpl implements UserService {

  @Autowired
  private UserRepository userRepository;

  @Autowired
  private RoleRepository roleRepository;
}

和这里的回购:

@Repository("userRepository")
public interface UserRepository extends CrudRepository<User, Long> {
     User findByEmail(String email);
}

当我运行应用程序时,我会收到以下消息:

Description:

Field userRepository in com.projectWS.service.UserServiceImpl required a 
bean of type 'com.projectWS.repo.UserRepository' that could not be found.

Action:

Consider defining a bean of type 'com.projectWS.repo.UserRepository' in your 
configuration.

请帮帮我,我很绝望... 这是我的主要课程:

@SpringBootApplication
@Configuration
@EnableWebMvc
public class Main {

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

另一个问题是,@EnableJpaRepositories 没有被识别!!

【问题讨论】:

  • “没有得到认可”是什么意思?听起来你可能没有包含对 Spring Data JPA 的依赖。 (并且在可行的情况下更喜欢构造函数注入而不是字段注入。)
  • 我在我的 pom.xml 中尝试了所有可能的依赖项......我不明白 paranths 之间的事情。请帮助我,我是今年春天的新手

标签: java spring spring-mvc spring-boot spring-security


【解决方案1】:

您似乎还没有为 JPA 添加 Spring 数据,请将以下内容添加到您的 pom.xml 中

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

【讨论】:

    【解决方案2】:

    很可能您的Main 类是在一个包中定义的,而您的其他包没有被扫描。

    尝试使用以下命令注释您的 Main 类:

    @ComponentScan("com.projectWS")
    

    根据您的错误消息判断并假设您的包级别的顶部从 com.projectWS 开始

    【讨论】:

    • 谢谢!!!我实际上是这样做的 @ComponentScan("com.projectWS.*") 并且像魅力一样工作
    【解决方案3】:

    我不是 Spring 专家,但我怀疑这可能是因为这些类的名称的大小写。只是为了安心测试,请:

    @Service("userService")
    public class UserServiceImpl implements UserService {
    
      @Qualifier("userRepository")
      @Autowired
      private UserRepository userRepository;
    
      @Qualifier("roleRepository")
      @Autowired
      private RoleRepository roleRepository;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-06-17
      • 2016-09-10
      • 2019-12-05
      • 2019-08-10
      • 1970-01-01
      • 2017-09-12
      • 2020-08-15
      • 2019-03-02
      • 1970-01-01
      相关资源
      最近更新 更多