最近在将之前遗留的一个数据服务项目从SSH移植到SpringBoot下,进行拆分作为单独的微服务进行运行。而在移植完成运行项目时,出现莫名异常,异常输出如下:
Description:
Field xxxxxxx_Repository in cn.cupid.service.impl.xxxxxxx_Service_Impl required a bean named 'mongoTemplate' 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 named 'mongoTemplate' in your configuration.
很明显,报的异常时是没有为其中一个Service的实现类注入mongoTemplate。于是考虑是否是自己代码中疏忽了,经过反复检查代码无问题。进而这种异常是否只有这个Service会出现呢?将这个Service有关的类注释之后,进行测试这个异常仍然存在。这个时候就可以确定了,是整个项目中的mongoTemplate注入失败。回顾代码,mongoTemplate确实已经被自己手动注入过,而且在之前的项目中是证实注入mongoTempate是成功的。我采用的注入方式是在SpringBoot项目入口类进行注入,注入代码如下:
package cn.cupid;
import com.mongodb.MongoClient;
import org.springframework.beans.fac