java - MapStruct 没有符合条件的 bean
问题描述
使用 MapStruct、Lombock 和 Spring 时,在 Tomcat 9 中部署应用程序时出现以下错误:
没有可用的“somepackage.ControllerBusinessMapper”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean
这是我的 pom.xml:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>[1.18.12,)</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.1.Final</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.1.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
这是映射器:
@Mapper(componentModel = "spring")
public interface ControllerBusinessMapper {
//Some methods
}
在其注入的类中:
@Autowired
private ControllerBusinessMapper businessMapper;
我的 spring 配置类在包层次结构的根中设置包扫描。而且映射器的实现是在目标/生成源下生成的:
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-08-23T03:56:23+0200",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.7 (Oracle Corporation)"
)
@Component
public class ControllerBusinessMapperImpl implements ControllerBusinessMapper {
//Some methods
}
我的错误表明 Spring 无法找到实现类,我错过了什么?我尝试将生成的源文件夹添加到构建路径并将其包含在包扫描中,但它不起作用。
解决方案
为了使用 MapStruct,需要在 Eclipse 中进行额外的配置:
- 安装 m2e-apt 插件
- 在每个 Maven 模块中,转到 Maven > Annotation Processing
- 选中启用项目特定设置
- 选择自动配置 JDT APT 模式
推荐阅读
- c# - Linq GetType GetProperties 基于 List
- java - 如何为if块抛出异常编写junit测试用例
- sql - 为什么使用变量时 SQL 函数需要这么长时间
- flutter - GetxController 不是 GetxController 的子类型
- resources - Anylogic - 创建资源并添加到 ResourcePool
- javascript - 从其声明中引用 javascript 对象
- python - 将新数据框附加到熊猫中的空数据框会提供额外的标头
- ag-grid - 抑制输入 - 在 ag-grid 中展开组
- three.js - three.js 可编辑的文本表单控件
- python - 我可以使用 python 程序并使用 tkinter 将其转换为 gui 吗?