先看我得项目结构,以及简单的配置

其中项目里使用到了Mysql 和 Mongo两个数据库

我们的项目是一个微服务项目。

报错展示,和分析
Description:
Field customerInfoDao in com.hariexpress.cloud.service.impl.chat.CustomerServiceImpl required a bean of type 'com.hariexpress.cloud.dao.mongo.CustomerInfoDao' that could not be found.
Action:
Consider defining a bean of type 'com.hariexpress.cloud.dao.mongo.CustomerInfoDao' in your configuration.
一开始我怀疑是,我注解的问题,我怀疑我在Dao层里加的@Repository是一个无效的,不会被Spring加载的注解,其实不是,Spring boot中,我们的application启动类有@SpringBootApplication这个注解,这个注解会扫描项目里所有的@Component注解并注册到容器,我查看了@Repository的源码,该注解被@Component所描述

也就是说@Repository被描述的类是可以被加载到容器的,那到底为什么没有被加载呢?
我开始怀疑是项目结构问题,service找Dao的时候就报了
百度了后我知道了个注解 @EnableMongoRepositories
@EnableMongoRepositories 的操作
百度一番之后,发现 @EnableMongoRepositories 注解其实和 @ComponentScan 是类似的,用于专门指定发现mongoDao层的类。了解到之后加上操作一番。结果如下:
代码:
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
@EnableMongoRepositories
public class WholesaleChatApplication {
public static void main(String[] args) {
SpringApplication.run(WholesaleChatApplication.class, args);
}
}
结果:
Description:
Field customerInfoDao in com.hariexpress.cloud.service.impl.chat.CustomerServiceImpl required a bean named 'mongoTemplate' that could not be found.
Action:
Consider defining a bean named 'mongoTemplate' in your configuration.
什么意思?mongoTemplate没有被发现,居然换了一个报错,那就再分析一下,到底什么情况才会出现 mongoTemplate that could not be found 呢?emmm..想了一下,mongoTemplate可是配置好 配置文件之后,添加了依赖之后,启动就会自动注入的呀。我去检查一下配置文件...

没问题呀!!!!!!!!!!!!思来想去,去检查我的依赖,尴尬原来是依赖没有添加,需要添加如下依赖,然后删除了 @EnableMongoRepositories 试试
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
结果:

居然好了 - -
网友评论