报错如下:
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 官网上是这么说的。至于为什么要这么搞,有待后续学习了
反思:
这个问题我查了一两个小时,但是查询的方向错误了,基本一直在查找博客。这还是学习方式和解决问题的方式有问题。
大佬出差回来后,咨询了一下大佬,大佬说看官方文档,然后找了一找,很快发现了解决问题的办法。
官方文档极其重要,推荐各位看客,遇到问题时先查找官方文档。我自身也要改变思路。