一、问题由来

  目前刚入职一家新公司不久,公司的新项目采用DDD驱动领域设计来进行开发,架构这一块使用的是阿里巴巴开源的最新框架COLA4.0的架构。

主要是这个框架里面的分层设计。主要分为四层:adapter适配层类似于以前的Controller层;app层类似于以前的Service层;domain层是领域层;还有

一个infrastructure层表示基础设施服务层,提供基础服务,比如数据库服务。项目还是进行设计阶段,自己主要负责开发这一块。由于大家都是刚开始

接触DDD这个新知识,因此还有很多东西都需要学习。由于我们负责的这个项目属于通用域,而且人手也不是太多,因此很多东西都需要亲力亲为。

比如说搭建项目框架,基础架构采用cola4.0,里面具体使用什么版本的SpringBoot,SpringCloud,Redis,Postgresql等等,都得我们自己来定。自己

也没闲着,立马去学习Cola4.0这个框架,由于这种设计理念很是新颖,因此在网上可供参考学习的资料很少。不像什么SpringBoot,springCloud之类的

课程,一搜就一堆的答案。Cola4.0的框架的大致架构如图所示:

详细的错误信息如下:

a component required a bean of type xxxxxx that could not be found
Action
Consider defining a bean of type xxxxxx in your configuration

二、问题分析

  从错误信息来看,大致意思就是组件找不到类型为xxxxxx的bean,可以考虑在配置中定义一个bean。自己根据实际情况来判断报错的类型是一个接口,

这个接口有具体的实现类,实现类中使用的注解为@Component.这个接口有在其他地方被使用,启动项目时由于这个类没有被加载到,因此报这个错。能

确定一点就是接口的实现类没有被加载到,可是为什么没有被加载到呢?框架是我们按照cola4.0的架构自己来搭建的,它里面就是一些简单的骨架,没有

具体封装一些代码之类的,我们用的话也只会考虑使用它的架构。这就很让人疑惑,使用原作者的框架就可以正常运行,怎么自己来搭建就不行了呢?

三、解决方案

拿到这个错误信息,自己立马开始使用搜索引擎。由于这个项目比较特殊,启动类是单独为一个start项目,主要作用就是用来启动

项目,不做其他任何事情。

尝试方案一:

推测接口实现类中没有添加对应的注解,所以不能被扫描到。

自己经过检查,发现所有的接口实现类都添加了@Component注解。会不会有这种可能,这个注解不生效?

于是自己换成@Service注解,重新启动项目,发现问题任然未解决。

参考文章 https://blog.csdn.net/qq_39691492/article/details/118085232

尝试方案二:

推测导致这个错误的原因可能是由于接口的实现类没有被扫描到,没有正确加载到spring容器中,最终出现问题。

使用@ComponentScan包扫描注解,把接口实现类的包路径放进去,然后重新启动项目。结果为解决。

参考文章 https://blog.csdn.net/suxiexingchen/article/details/84984349

尝试方案三:

推测可能是包结构不对导致来加载错误。这到是有可能,然后自己仔细检查了项目的包结构,排除此问题,包结构

没有问题,还是没有解决。

https://blog.csdn.net/Julycaka/article/details/80622754

这时候自己就开始分析,说先确定一点接口的实现类没有被加载到spring容器中,为什么会没有被加载到呢?然后自己

倒回去看看cola4.0的例子,查看模块之间的相互引用关系,这时发现问题了。demo例子中app层直接引用了infrastructure,

我们自己搭建的框架中没有做这个引用,这就导致infrastructure层中的代码没有被正确扫描到,继而不能被加载到spring容器中。

自己立马去app层中添加该模块,然后重新启动项目,发现启动成功,问题解决。

总结:使用maven进行多模块开发的时候,需要确保所有的模块中的包路径都能被扫描到,否则就会导致类加载不了的问题。

不管是直接引用还是间接引用,只有所有的模块都和启动类关联起来后,项目才能够正常启动。我们的项目中引用关系如下:

start层 引用 adapter层;

adapter层 引用 app层;

app层 引用 domain层;app层 引用 infrastructure层;

infrastructure层 引用 domain层;

common层被除start层外的所有模块引用;

这样虽然项目启动层start中只有一个启动类,通过这种直接的或者是间接的引用关系就可以加载所有的bean对象。

a commponent required a bean of type XXXXXX that could not be found-2022新项目的更多相关文章

  1. Field redisTemplate in xxxxxx required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found.

    *************************** APPLICATION FAILED TO START *************************** Description: Fie ...

  2. Sping Cloud项目启动报A component required a bean of type 'com.tianyan.bbc.dao.SecurityUserBaseMapper' that could not be found.

    项目构建正常,启动和Debug报以下错误: Error starting ApplicationContext. To display the conditions report re-run you ...

  3. 2. springboot启动报错:Field userMapper in com.service.UserService required a bean of type 'com.dao.UserMapper' that could not be found.

    报错信息: 2018-06-25 14:26:17.103  WARN 49752 --- [  restartedMain] ationConfigEmbeddedWebApplicationCon ...

  4. SpringCloud报错: "Field discoveryClient in com.controller.DcController required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found."

    SpringCloud报错: "Field discoveryClient in com.controller.DcController required a bean of type 'c ...

  5. spring eureka required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found.

    spring在集成第三方过程很容易出现类名相同,且基本作用相同的类.这样给初学者带来一定的困惑. 导致用错类而出现以下问题. required a bean of type 'com.netflix. ...

  6. maven多模块启动required a bean of type com.xxx.xxx.service that could not be found.

    Description: Field testService in com.xxx.xxx.api.controller.TestController required a bean of type ...

  7. Field userService in com.wuji.controller.UserController required a bean of type 'com.wuji.service.UserService' that could not be found

    Field userService in com.wuji.controller.UserController required a bean of type 'com.wuji.service.Us ...

  8. 解决办法 Field userService in com.sxsj.controller.RegistLoginController required a bean of type

    转自:https://blog.csdn.net/awmw74520/article/details/82687288 APPLICATION FAILED TO START Error starti ...

  9. Spring Cloud Ribbon负载均衡配置类放在Spring boot主类同级增加Exclude过滤后报Field config in com.cloud.web.controller.RibbonConfiguration required a bean of type 'com.netflix.client.config.IClientConfig' that could not b

    环境: Spring Cloud:Finchley.M8 Spring Boot:2.0.0.RELEASE 目录结构: 可以看到代码第13行的注释,我已经在@ComponentScan注解中添加了E ...

随机推荐

  1. 用AltiumDesigner画完PCB生成gerber(光绘)文件详细说明

    什么是gerber文件             Gerber文件是所有电路设计软件都可以产生的文件,在电子组装行业又称为模版文件(stencil data),在PCB制造业又称为光绘文件.可以说Ger ...

  2. HTML5 localStorage使用方法及注意点

    html5新增了在客户端存储数据的新方法:1.localStorage - 没有时间限制的数据存储:2.sessionStorage - 针对一个session的数据存储,当用户关闭浏览器窗口后,数据 ...

  3. 深入理解ES6之《ES7》

    指数运算符 Math.pow是可以进行求幂运算的求幂运算符是两个星号 let result = 5 ** 2 console.log(result) //25 console.log(result = ...

  4. vue中事件冒泡规则和事件捕获规则

    <div id="app"> <div @click="handleClickOne"> <p @click="hand ...

  5. CSS简单样式练习(四)

    运行效果: 源代码: 1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta char ...

  6. 在react项目中使用redux-thunk,react-redux,redux;使用总结

    先看是什么,再看怎么用: redux-thunk是一个redux的中间件,用来处理redux中的复杂逻辑,比如异步请求: redux-thunk中间件可以让action创建函数先不返回一个action ...

  7. MySQL安装配置教程(超级详细)

    一. 下载MySQL Mysql官网下载地址:https://downloads.mysql.com/archives/installer/ 1. 选择要安装的版本,本篇文章选择的是5.7.31版本, ...

  8. 关于#pragma 和 _pragma

    首先要明确 #pragma 和_Pragma 是什么 这两个都是出自于c/c++ 的 ,其中#pragma 是预处理指令(preProcess directive ) ,#pragma是用来向编译器传 ...

  9. Spring配置文件-Bean实例化的三种方式

    1.无参构造方法实例化(详见我的博客) 2.工厂静态方法实例化 创建StaticFactory类 public class StaticFactory { public static UserDao ...

  10. 宝藏考研公众号,考研up篇(参考)

    每当刷完题感觉到累了,看完网课觉得倦了,拿起手机看一下我推荐的宝藏公众号和up推送的文章和视频(。・ω・。)ノ♡,这样既可以换换口味解解腻,又可以不断的提升自己,岂不美哉?让别人以为你拿起手机开始摆烂 ...