当前位置:网站首页>记录一些遇见的bug——Lombok和Mapstruct的冲突导致,A component required a bean of type ‘com.XXX.controller.converter.
记录一些遇见的bug——Lombok和Mapstruct的冲突导致,A component required a bean of type ‘com.XXX.controller.converter.
2022-08-11 07:04:00 【叶不修233】
记录一些遇见的bug——Lombok和Mapstruct的冲突导致,A component required a bean of type 'com.XXX.controller.converter.BrandConverter' that could not be found.
一、问题描述
报错截图:
如果你的项目同时使用了Lombok和Mapstruct工具,写了dto转换类,例如下面的BrandConverter转换类
@Component
@Mapper(componentModel = "spring")
public interface BrandConverter {
PageInfo<BrandDto> toDto(PageInfo<BrandPo> brandPoPageInfo);
}
明明为BrandConverter转换类配置了@Component注解,但idea提示:
A component required a bean of type ‘com.XXX.controller.converter.BrandConverter’ that could not be found.
未找到BrandConverter的bean
二、原因和解决方案
由于 MapStruct 依赖于 JavaBean 中有 getter/setter 方法,所以,如果使用了 lombok 来生成 getter/setter 方法的话,那么在配置上有一些特殊的地方:需要 lombok 先对 .class 文件"动手脚",然后 MapStruct 再来对 .class 文件"动手脚"。
我们需要在 maven 项目的 pom.xml 中的 plugins > plugin 下的 maven-compiler-plugin 插件下加上好大一坨 configuration 配置。
如图所示:

<properties>
<org.mapstruct.version>1.5.0.RC1</org.mapstruct.version>
<org.projectlombok.version>1.18.20</org.projectlombok.version>
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
... 其它版本声明
</properties>
<dependencies>
<!-- lombok dependencies should not end up on classpath -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
...其它依赖声明
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok-mapstruct-binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
...其它插件声明
</plugins>
</build>
clean之后重新编译,再次启动,不再报错。
边栏推荐
- Serverless + domain name can also build a personal blog? Really, and soon
- 关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来
- Activity的四种状态
- 1076 Wifi Password (15 points)
- oracle19c does not support real-time synchronization parameters, do you guys have any good solutions?
- 机器学习总结(二)
- CIKM 2022 AnalytiCup Competition: 联邦异质任务学习
- 【LeetCode每日一题】——682.棒球比赛
- Four startup modes of Activity
- Conditional statements in TF; where()
猜你喜欢
随机推荐
Activity的四种状态
关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来
How Unity programmers can improve their abilities
Two state forms of Service
Unity3D learning route?
[Recommender System]: Overview of Collaborative Filtering and Content-Based Filtering
求职简历这样写,轻松搞定面试官
How Unity handles C# under the hood
详述MIMIC 的ICU患者检测时间信息表(十六)
关于Android Service服务的面试题
LeetCode brushing series -- 46. Full arrangement
1051 复数乘法 (15 分)
Test cases are hard?Just have a hand
无服务器+域名也能搭建个人博客?真的,而且很快
1076 Wifi密码 (15 分)
流式结构化数据计算语言的进化与新选择
2.1-梯度下降
从苹果、SpaceX等高科技企业的产品发布会看企业产品战略和敏捷开发的关系
1061 判断题 (15 分)
【sdx62】XBL设置共享内存变量,然后内核层获取变量实现







