Springboot 使用JPA Repository 注入失败问题 - Go语言中文社区

Springboot 使用JPA Repository 注入失败问题


报错如下:

APPLICATION FAILED TO START
***************************

Description:

Field customerRepository in com.xx.xx.xx.customer.service.impl.CustomerServiceImpl required a bean of type 'com.xx.xx.xx.customer.repository.CustomerRepository' 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.xx.xx.xx.customer.repository.CustomerRepository' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:61358', transport: 'socket'

Process finished with exit code 1

环境如下:

springboot 2.1.6.RELEASE。

 

springboot 启动类配置如下:

package com.xx.xx.xx.application;

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
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.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@NacosPropertySource(dataId = "xx, autoRefreshed = true)
@EnableDubbo
@EnableAsync
@EnableSwagger2Doc
@EnableJpaAuditing
// TODO 问题原因在这里,需要手动开启 JpaRepositories 的注入
// @EnableJpaRepositories(basePackages = OmsApplication.xxPACKAGES)
@EntityScan({OmsApplication.xxPACKAGES})
@ComponentScan(basePackages = {OmsApplication.xxPACKAGES})
public class OmsApplication {
    static final String xxPACKAGES = "com.xx.*";

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

}

原因:

https://docs.spring.io/spring-data/jpa/docs/2.1.6.RELEASE/reference/html/#repositories 

springboot 官网上是这么说的。至于为什么要这么搞,有待后续学习了

 反思:

这个问题我查了一两个小时,但是查询的方向错误了,基本一直在查找博客。这还是学习方式和解决问题的方式有问题。

大佬出差回来后,咨询了一下大佬,大佬说看官方文档,然后找了一找,很快发现了解决问题的办法。

官方文档极其重要,推荐各位看客,遇到问题时先查找官方文档。我自身也要改变思路。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/H_Rhui/article/details/103391794
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-28 01:17:12
  • 阅读 ( 1124 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢